Skip to content

Commit 1a5a314

Browse files
committed
Add linkedlist listiterator example
1 parent 4677def commit 1a5a314

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)