Java initialize ArrayList example shows how to initialize ArrayList in Java using various approaches. It also shows how to initialize ArrayList with all zeroes.
How to initialize ArrayList in Java?
Normally we create and add elements to the ArrayList as given below.
1 2 3 4 5 6 7 |
//create ArrayList ArrayList<String> aListDays = new ArrayList<String>(); //initialize aListDays.add("Sun"); aListDays.add("Mon"); aListDays.add("Tue"); |
This works perfectly for the ArrayList declared inside the methods. But what if the ArrayList is a member variable declared at the class level and we want to make sure that it is initialized before it is accessed.
In such cases, you can use any of the below given approaches to initialize the ArrayList with default elements.
1) Using an anonymous inner class
You can use an anonymous inner class with instance initializer as given below.
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 com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class InitializeArrayListExample { private ArrayList<String> aListDays = new ArrayList<String>(){{ add("Sun"); add("Mon"); add("Tue"); }}; public static void main(String[] args) { new InitializeArrayListExample().printList(); } public void printList(){ for(String day : aListDays) System.out.println(day); } } |
Output
1 2 3 |
Sun Mon Tue |
2) Using a static block
You can use a static block to initialize the ArrayList with default elements. The static blocks are executed when the class is first loaded into the memory.
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 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class InitializeArrayListExample { private ArrayList<String> aListDays = new ArrayList<String>(); //static block to initialize ArrayList { aListDays.add("Sun"); aListDays.add("Mon"); aListDays.add("Tue"); } public static void main(String[] args) { new InitializeArrayListExample().printList(); } public void printList(){ for(String day : aListDays) System.out.println(day); } } |
Output
1 2 3 |
Sun Mon Tue |
3) Using the Arrays class
You can also use the asList
method of the Arrays class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; import java.util.Arrays; public class InitializeArrayListExample { private ArrayList<String> aListDays = new ArrayList<String>(); public static void main(String[] args) { ArrayList<String> aListDays = new ArrayList<String>( Arrays.asList("Sun", "Mon", "Tue") ); for(String day : aListDays) System.out.println(day); } } |
Output
1 2 3 |
Sun Mon Tue |
4) Using Java 8 stream
If you are using Java 8, you can use a stream as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class InitializeArrayListExample { public static void main(String[] args) { List<String> aListDays = Stream.of("Sun", "Mon", "Tue").collect(Collectors.toList()); for(String day : aListDays) System.out.println(day); } } |
Output
1 2 3 |
Sun Mon Tue |
The above code returns an unmodifiable list that you cannot change in any way. If you need to change it, use the below code instead.
1 2 3 4 5 |
ArrayList<String> aListDays = Stream.of("Sun", "Mon", "Tue").collect(Collectors.toCollection(ArrayList::new)); for(String day : aListDays) System.out.println(day); |
5) Using Collections class
You can use addAll
method of Collections class to add many values at once to the ArrayList as given below.
1 2 3 4 5 |
ArrayList<String> aListDays = new ArrayList<String>(); Collections.addAll(aListDays, "Sun", "Mon", "Tue"); for(String day : aListDays) System.out.println(day); |
How to initialize an ArrayList with all zeros?
Consider below given code to create an ArrayList.
1 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>(10); |
The above code creates a new ArrayList object with an initial capacity of 10. Now you can use nCopies
method of the Collections class to fill the ArrayList elements with zeros.
1 |
public static <T> List<T> nCopies(int n, T o) |
This method returns a list containing the specified object n times.
1 2 3 4 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>( Collections.nCopies(10, 0) ); System.out.println(Arrays.toString( aListNumbers.toArray())); |
Output
1 |
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
This example is a part of the ArrayList in Java Tutorial.
Please let me know your views in the comments section.