ArrayList in Java
here is the way to initialize array method .
syntax
ArrayList<T> obj = new ArrayList<T>(){{
add(Object o1);
add(Object o2);
add(Object o3);
...
...
}};
import java.util.ArrayList;
public class Example1 {
public static void main(String args[]) {
// DataType of Object is String that we initialize array
ArrayList<String> arr = new ArrayList<String>(){{
add("Item1");
add("Item2");
add("Item3");
}};
System.out.println("Content of Array list cities:"+arr);
}
}
//result below:
//Content of Array list cities:[Item1, Item2, Item3]
We see datatype is object1, object2, and object3 are String and we add String , item1, item2, item3 to arraylist
EmoticonEmoticon