190
Q:

java array to arraylist

Integer[] spam = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(spam);
9
List<Element> list = Arrays.asList(array);
4
List<String> list = new ArrayList<>();
list.add("a");
list.add("ab");
list.add("abc");
list.add("abcd");
// convert
String[] array = list.toArray();
5
/*
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);
3
ArrayList<String> ArrayLister=new ArrayList<>(){ArrayLister.add("Hi")};
String[] array=ArrayLister.toArray(String[]);
2
// Java Program to illustrate the 
// ArrayList toArray() method in Java 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create object of ArrayList 
        ArrayList<Integer> ArrLis 
            = new ArrayList<Integer>(); 
  
        // Add elements 
        ArrLis.add(32); 
        ArrLis.add(67); 
        ArrLis.add(98); 
        ArrLis.add(100); 
  
        // print ArrayList 
        System.out.println("ArrayList: "
                           + ArrLis); 
  
        // Get the array of the elements 
        // of the ArrayList 
        // using toArray() method 
        Object[] arr = ArrLis.toArray(); 
  
        System.out.println("Elements of ArrayList"
                           + " as Array: "
                           + Arrays.toString(arr)); 
    } 
} 
1
Integer[] arr = new Integer[al.size()]; 
  
        // ArrayList to Array Conversion 
        for (int i = 0; i < al.size(); i++) 
            arr[i] = al.get(i); 
0
Integer[] arr = new Integer[al.size()]; 
for (int i = 0; i < al.size(); i++) 
     arr[i] = al.get(i); 
0

New to Communities?

Join the community