Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2e3b929

Browse files
Add coding examples
1 parent b5ad313 commit 2e3b929

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
/**
5+
* An example java program for ensureCapacity(int MinCapacity) method
6+
* of the ArrayList class in java.
7+
* @author Gaurav at coderolls.com
8+
*
9+
*/
10+
public class ArrayListEnsureCapacityExample {
11+
12+
public static void main(String[] args) {
13+
14+
// create an empty arraylist object 'states'
15+
ArrayList<String> states = new ArrayList<>();
16+
17+
// add state in the arraylist
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+
states.add("Minnesota");
28+
states.add("Colorado");
29+
states.add("Missouri");
30+
states.add("Nevada");
31+
System.out.println("ArrayList Elements are: \n"+ states);
32+
//We need to add more states, so we will ensure
33+
//that arraylist should hold 50 elements
34+
states.ensureCapacity(50);
35+
36+
System.out.println("\nWe ensured that the arraylist should hold 50 elements.");
37+
}
38+
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.ListIterator;
5+
/**
6+
* An example java program about the listIterator()
7+
* method of the arrayList.
8+
*
9+
* @author coderolls.com
10+
*/
11+
public class ArrayListListIteratorExample {
12+
13+
public static void main(String[] args) {
14+
15+
ArrayList<String> states = new ArrayList<>();
16+
17+
// add state in the arraylist
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+
ListIterator<String> itr = states.listIterator();
29+
30+
while(itr.hasNext()) {
31+
String state = itr.next();
32+
System.out.println("The state :"+ state);
33+
}
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.ListIterator;
5+
/**
6+
* An example java program about the listIterator()
7+
* method of the arrayList.
8+
*
9+
* @author coderolls.com
10+
*/
11+
public class ArrayListListIteratorExample2 {
12+
13+
public static void main(String[] args) {
14+
15+
ArrayList<String> states = new ArrayList<>();
16+
17+
// add state in the arraylist
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+
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.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.ListIterator;
5+
/**
6+
* An example java program about the listIterator()
7+
* method of the arrayList.
8+
*
9+
* @author coderolls.com
10+
*/
11+
public class ArrayListListIteratorException {
12+
13+
public static void main(String[] args) {
14+
15+
ArrayList<String> states = new ArrayList<>();
16+
17+
// add state in the arraylist
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)

AltStyle によって変換されたページ (->オリジナル) /