File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed
arraylist-ensurecapacity-method
arraylist-listiterator-method Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
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 ("\n We ensured that the arraylist should hold 50 elements." );
37
+ }
38
+
39
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments