File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
collections/linkedlist/linkedlist-listiterator Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .ListIterator ;
5+ /**
6+ * An example java program about the listIterator(int index)
7+ * method of the linkedList.
8+ *
9+ * @author coderolls.com
10+ */
11+ public class LinkedListListIteratorExample {
12+
13+ public static void main (String [] args ) {
14+
15+ LinkedList <String > states = new LinkedList <>();
16+
17+ // add state in the linkedList
18+ states .add ("California" );
19+ states .add ("Texas" );
20+ states .add ("Montana" );
21+ states .add ("Arizona" );
22+ states .add ("Florida" );
23+ states .add ("Michigan" );
24+ states .add ("New Jersey" );
25+ states .add ("Washington" );
26+ states .add ("Ohio" );
27+
28+ //given index 3, iterator start from element with index 3
29+ ListIterator <String > itr = states .listIterator (3 );
30+ System .out .println ("Iterating the list from index 3\n " );
31+ while (itr .hasNext ()) {
32+ String state = itr .next ();
33+ System .out .println ("The state :" + state );
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+ import java .util .ListIterator ;
5+ /**
6+ * An example java program about the listIterator()
7+ * method of the linkedList.
8+ *
9+ * @author coderolls.com
10+ */
11+ public class LinkedListListIteratorException {
12+
13+ public static void main (String [] args ) {
14+
15+ LinkedList <String > states = new LinkedList <>();
16+
17+ // add state in the linkedList
18+ states .add ("California" );
19+ states .add ("Texas" );
20+ states .add ("Montana" );
21+ states .add ("Arizona" );
22+ states .add ("Florida" );
23+ states .add ("Michigan" );
24+ states .add ("New Jersey" );
25+ states .add ("Washington" );
26+ states .add ("Ohio" );
27+
28+ //given index 120, it will throw IndexOutOfBoundsException
29+ ListIterator <String > itr = states .listIterator (120 );
30+
31+ while (itr .hasNext ()) {
32+ String state = itr .next ();
33+ System .out .println ("The state :" + state );
34+ }
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments