Earlier we have seen Arrays and we found few limitations of Arrays in Java. Size of an Array is fixed.

Using ArrayList we can overcome the size issue. ArrayList is a resizable array.

ArrayList Class implements List interface. ArrayList allows duplicate elements (remember that Set in Java wont allow duplicate values. We will see Set in later sections)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
ArrayList arrList = new ArrayList();
// We can use any data type
arrList.add(987654321);
// Here i am passing next two values as “STM” just to show you that List allows duplicate values..
// Set wont allow duplicate values.
// Adding elements to the array list
arrList.add(“STM”);
arrList.add(“STM”);
arrList.add(99.99);
// To get all the values from the list
System.out.println(arrList);
// To get a value whose index is 1
System.out.println(arrList.get(1));
 
}
 
}

Output:

1
2
[987654321, STM, STM, 99.99]
STM

ArrayList using For loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
ArrayList arrList = new ArrayList();
// Adding elements to the array list
arrList.add(987654321);
arrList.add(“STM”);
arrList.add(“STM”);
arrList.add(99.99);
int listSize = arrList.size();
for(int i=0; i<listSize; i++){
System.out.println(“Value of Array List at the index “+i+” is “+arrList.get(i));
}
 
}
 
}

Output:

1
2
3
4
Value of Array List at the index 0 is 987654321
Value of Array List at the index 1 is STM
Value of Array List at the index 2 is STM
Value of Array List at the index 3 is 99.99

ArrayList with For Each Loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
ArrayList arrList = new ArrayList();
// Adding elements to the array list
arrList.add(987654321);
arrList.add(“STM”);
arrList.add(99.99);
for(Object arrListItem : arrList){
System.out.println(“Value of Array List at the index “+arrList.indexOf(arrListItem)+” is “+arrListItem);
}
 
}
 
}

Output:

1
2
3
Value of Array List at the index 0 is 987654321
Value of Array List at the index 1 is STM
Value of Array List at the index 2 is 99.99

ArrayList using Iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
ArrayList arrList = new ArrayList();
// Adding elements to the array list
arrList.add(987654321);
arrList.add(“STM”);
arrList.add(99.99);
Iterator itr = arrList.iterator();
while(itr.hasNext()){
Object value=itr.next();
System.out.println(“Values are “+value);
}
 
}
 
}

Output:

1
2
3
Values are 987654321
Values are STM
Values are 99.99

Creation of ArrayList with String elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
// Creation of ArrayList with String elements
ArrayList <String> arrList = new ArrayList<String>();
// Adding elements to the array list
arrList.add(“STM”);
arrList.add(“Software Testing Material”);
for(String arrListItem:arrList){
System.out.println(arrListItem);
}
 
}
 
}

Output:

1
2
STM
Software Testing Material

Adding elements at a given index using ArrayList:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package classFiveArrays;
 
import java.util.ArrayList;
import java.util.Iterator;
 
public class ArrayListClass {
public static void main(String [] args){
// How to add elements at a given index using ArrayList
// Creation of ArrayList with String elements
ArrayList <String> arrList = new ArrayList<String>();
// Adding elements to the array list
arrList.add(0, “STM”);
arrList.add(1, “Software Testing Material”);
System.out.println(arrList);
//Remove elements from array list like this
arrList.remove(“Software Testing Material”);
System.out.println(arrList);
 
arrList.add(1, “Software Testing Material”);
System.out.println(arrList);
arrList.remove(1);
System.out.println(arrList);
 
}
 
}

Output:

1
2
3
4
[STM, Software Testing Material]
[STM]
[STM, Software Testing Material]
[STM]

Must Read: Java Tutorial