1. Using add() method
2. Using Arrays.asList() method
3. Using Anonymous inner class method
4. Using Collections.nCopies() method
Read Also: How to Reverse an ArrayList in Java
Initialize ArrayList in Java
Method 1: Using add() method
Syntax:
a. Initialize ArrayList of String using add() method:
ArrayList<String> al = new ArrayList();
al.add("String1");
al.add("String2");
al.add("String3");
b. Initialize ArrayList of Integer using add() method:
ArrayList<Integer> al2 = new ArrayList();
al2.add(Integer1);
al2.add(Integer2);
al2.add(Integer3);
Example:
Given below is the example of initializing ArrayList using add() method:
import java.util.*;
public class ArrayListInitialization {
public static void main(String args[]) {
ArrayList<String> cars = new ArrayList();
cars.add("Honda");
cars.add("Hyundai");
cars.add("Toyota");
System.out.println("Cars stored in ArrayList are: "+ cars);
}
}
Output:
Cars stored in ArrayList are: [Honda, Hyundai, Toyota]
Method 2: Using Arrays.asList() method
Syntax:
a. Initialize ArrayList of String using Arrays.asList() method:
ArrayList<String> al = new ArrayList(Arrays.asList("String1","String2","String3"));
b. Initialize ArrayList of Integer using Arrays.asList() method:
ArrayList<Integer> al2 = new ArrayList(Arrays.asList(Integer1, Integer2, Integer3));
Example:
Given below is the example of initializing ArrayList using Arrays.asList() method:
import java.util.*;
public class ArrayListInitialization2 {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList(Arrays.asList("Boston", "Chicago", "Dallas"));
System.out.println("Elements are: "+ al);
}
}
Output:
Elements are: [Boston, Chicago, Dallas]
Method 3: Using Anonymous inner class method
Syntax:
Initialize ArrayList of String using Anonymous inner class method:
ArrayList<String> al = new ArrayList(){{
add("String1");
add("String2");
add("String3");
add("String4");
}};
Example:
Given below is the example of initializing ArrayList using anonymous inner class method:
import java.util.ArrayList;
public class ArrayListInitialization3 {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList(){{
add("Facebook");
add("TikTok");
add("Twitter");
add("Snapchat");
}};
System.out.println("Best social media platforms are: "+ al);
}
}
Output:
Best social media platforms are: [Facebook, TikTok, Twitter, Snapchat]
Method 4: Using Collections.nCopies() method
Syntax:
a. Initialize ArrayList of String using Collections.nCopies() method:
ArrayList<String> al = new ArrayList(Collections.nCopies(count, element));
Example:
Given below is the example of initializing ArrayList using Collections.nCopies():
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListInitialization4 {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList(Collections.nCopies(7, 7));
System.out.println("nCopies prints elements are: "+ al);
}
}
Output:
nCopies prints elements are: [7, 7, 7, 7, 7, 7, 7]
That's all for today. Please mention in the comments if you know any other way of initializing an ArrayList in Java.
0 Commentaires