转载:https://beginnersbook.com/2013/12/how-to-initialize-an-arraylist/
Method 1: Initialization using Arrays.asList
Syntax:
ArrayList<Type> obj = new ArrayList<Type>( Arrays.asList(Object o1, Object o2, Object o3, ....so on));
Example:
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); } }
Output:
Elements are:[Pratap, Peter, Harsh]
Method 2: Anonymous inner class method to initialize ArrayList
Syntax:
ArrayList<T> obj = new ArrayList<T>(){{ add(Object o1); add(Object o2); add(Object o3); ... ... }};
Example:
import java.util.*; public class InitializationExample2 { public static void main(String args[]) { ArrayList<String> cities = new ArrayList<String>(){{ add("Delhi"); add("Agra"); add("Chennai"); }}; System.out.println("Content of Array list cities:"+cities); } }
Output:
Content of Array list cities:[Delhi, Agra, Chennai]
Method3: Normal way of ArrayList initialization
Syntax:
ArrayList<T> obj = new ArrayList<T>(); obj.add("Object o1"); obj.add("Object o2"); obj.add("Object o3"); ... ...
Example:
import java.util.*; public class Details { public static void main(String args[]) { ArrayList<String> books = new ArrayList<String>(); books.add("Java Book1"); books.add("Java Book2"); books.add("Java Book3"); System.out.println("Books stored in array list are: "+books); } }
Output:
Books stored in array list are: [Java Book1, Java Book2, Java Book3]
Method 4: Use Collections.ncopies
Collections.ncopies method can be used when we need to initialize the ArrayList with the same value for all of its elements. Syntax: count is number of elements and element is the item value
ArrayList<T> obj = new ArrayList<T>(Collections.nCopies(count, element));
Example:
import java.util.*; public class Details { public static void main(String args[]) { ArrayList<Integer> intlist = new ArrayList<Integer>(Collections.nCopies(10, 5)); System.out.println("ArrayList items: "+intlist); } }
Output:
ArrayList items: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]