how to initialize an arraylist in java code example

Example 1: java instantiate list with value

List<String> strings = Arrays.asList("foo", "bar", "baz");

Example 2: {} when initialising arraylist java

ArrayList<Type> obj = new ArrayList<Type>(
        Arrays.asList(Object o1, Object o2, Object o3, ....so on));

Example 3: initialize arraylist

ArrayList<Type> list = new ArrayList<Type>(Arrays.asList(
		elem1, elem2,..., elemN
        ));

Example 4: {} when initialising arraylist java

import java.util.*;
public class InitializationExample1 {
   public static void main(String args[]) {
	   ArrayList<String> obj = new ArrayList<String>(
		Arrays.asList("Pratap", "Peter", "Harsh"));
	  System.out.println("Elements are:"+obj);
   }
}

Example 5: initialiser une arraylist

ArrayList couleurs = new ArrayList() {{
 add(Rouge);
 add(Bleu);
 add(Vert);
}};

Example 6: java initalize arraylist

List<Type> list = Arrays.asList(elem1, elem2,..., elemN);

Tags:

Java Example