In this tutorial we will learn ArrayList in java with example
In Java we can achieve the dynamic array using arraylist.
ArrayList class available on util package, so we don’t need to import any extra packages, In below example
- created new arraylist
- added value to arraylist
- printing the array list value by using for loop
Example
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List arrayList = new ArrayList();
arrayList.add("Java");
arrayList.add("Java 8");
arrayList.add("Java 11");
for (int i = 0; i < arrayList.size(); i++) {
System.out.println("Value at the index of " + i + " "+arrayList.get(i));
}
}
}
Output
Github
Related Articles
Sort an Arraylist in java with example
iterate stream with index in Java8