xxxxxxxxxx
// Java program to demonstrate working of
// Objectp[] toArray()
import java.io.*;
import java.util.List;
import java.util.ArrayList;
class GFG {
public static void main(String[] args)
{
List<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
Object[] objects = al.toArray();
// Printing array of objects
for (Object obj : objects)
System.out.print(obj + " ");
}
}
xxxxxxxxxx
List<String> list = new ArrayList<>();
list.add("pizza");
list.add("dosa");
list.add("biryani");
//returns an array of String, hence better!!
String[] array1 = list.toArray(new String[0]);
//returns an array of Object
Object[] array2 = list.toArray();
xxxxxxxxxx
List<String> list = new ArrayList<>();
list.add("a");
list.add("ab");
list.add("abc");
list.add("abcd");
// convert
String[] array = list.toArray();
xxxxxxxxxx
List<String> list=new ArrayList<String>();
list.add("sravan");
list.add("vasu");
list.add("raki");
// Conversion part
String names[]=list.toArray(new String[list.size()])
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
Integer[] arr = new Integer[al.size()];
// ArrayList to Array Conversion
for (int i = 0; i < al.size(); i++)
arr[i] = al.get(i);
xxxxxxxxxx
Integer[] arr = new Integer[al.size()];
for (int i = 0; i < al.size(); i++)
arr[i] = al.get(i);
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);
}
}