xxxxxxxxxx
String[] arr = { 1, 2, 3, 4 }
ArrayList<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, arr);
// 'list' now contains [1, 2, 3, 4]
xxxxxxxxxx
/*
Get the Array to be converted.
Create the List by passing the Array as parameter in the constructor of the List with the help of Arrays. asList() method.
Return the formed List.
*/
String[] namedata = { "ram", "shyam", "balram" };
List<String> list = Arrays.asList(namedata);
xxxxxxxxxx
Integer[] array = new Integer[] {
23, 54, 12
};
java.util.List<Integer> list = java.util.Arrays.asList(array);
System.out.println(list);
xxxxxxxxxx
String[] myArray = new String[] { "I", "like", "eating", "pizza" };
List<String> myList = Arrays.asList(myArray);
myList.forEach(string -> System.out.println(string));
// -output-
// I
// like
// eating
// pizza
// -output-
xxxxxxxxxx
// Java program to demonstrate conversion of
// Array to ArrayList of fixed-size.
import java.util.*;
class GFG
{
public static void main (String[] args)
{
String[] geeks = {"Rahul", "Utkarsh",
"Shubham", "Neelam"};
// Conversion of array to ArrayList
// using Arrays.asList
List al = Arrays.asList(geeks);
System.out.println(al);
}
}
xxxxxxxxxx
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());
System.out.println(list); // Output: [1, 2, 3]
}
}
xxxxxxxxxx
ArrayList<Integer> list = new ArrayList<>(OtherList);
//it will copy all elements from OtherList to this one
//time complexity - O(n)