Element[] array = {new Element(1), new Element(2), new Element(3)};
How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>?
ArrayList<Element> arrayList = ...;
Element[] array = {new Element(1), new Element(2), new Element(3)};
How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>?
ArrayList<Element> arrayList = ...;
There is one more way that you can use to convert the array into an ArrayList. You can iterate over the array and insert each index into the ArrayList and return it back as in ArrayList.
This is shown below.
public static void main(String[] args) {
String[] array = {new String("David"), new String("John"), new String("Mike")};
ArrayList<String> theArrayList = convertToArrayList(array);
}
private static ArrayList<String> convertToArrayList(String[] array) {
ArrayList<String> convertedArray = new ArrayList<String>();
for (String element : array) {
convertedArray.add(element);
}
return convertedArray;
}
In java there are mainly 3 methods to convert an array to an arrayList
Using Arrays.asList() method : Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.
List<String> list = Arrays.asList(array);
System.out.println(list);
Collections.addAll() method - Create a new list before using this method and then add array elements using this method to existing list.
List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, array);
System.out.println(list1);
Iteration method - Create a new list. Iterate the array and add each element to the list.
List<String> list2 = new ArrayList<String>();
for(String text:array) {
list2.add(text);
}
System.out.println(list2);
you can refer this document too
You can use the following 3 ways to create ArrayList from Array.
String[] array = {"a", "b", "c", "d", "e"};
//Method 1
List<String> list = Arrays.asList(array);
//Method 2
List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, array);
//Method 3
List<String> list2 = new ArrayList<String>();
for(String text:array) {
list2.add(text);
}
You can create an ArrayList using Cactoos (I'm one of the developers):
List<String> names = new StickyList<>(
"Scott Fitzgerald", "Fyodor Dostoyevsky"
);
There is no guarantee that the object will actually be of class ArrayList. If you need that guarantee, do this:
ArrayList<String> list = new ArrayList<>(
new StickyList<>(
"Scott Fitzgerald", "Fyodor Dostoyevsky"
)
);
I've used the following helper method on occasions when I'm creating a ton of ArrayLists and need terse syntax:
import java.util.ArrayList;
import java.util.Arrays;
class Main {
@SafeVarargs
public static <T> ArrayList<T> AL(T ...a) {
return new ArrayList<T>(Arrays.asList(a));
}
public static void main(String[] args) {
var al = AL(AL(1, 2, 3, 4), AL(AL(5, 6, 7), AL(8, 9)));
System.out.println(al); // => [[1, 2, 3, 4], [[5, 6, 7], [8, 9]]]
}
}
Guava uses the same approach so @SafeVarargs appears to be safe here. See also Java SafeVarargs annotation, does a standard or best practice exist?.