package com.usr.collections;
import java.util.ArrayList;
import java.util.List;
public class ListToArray {
public static void main(String[] args) {
List<String> listDays = new ArrayList<String>();
listDays.add("sunday");
listDays.add("monday");
listDays.add("tuesday");
listDays.add("wednesday");
listDays.add("thursday");
listDays.add("friday");
listDays.add("saturday");
String[] arrDays = listDays.toArray(new String[listDays.size()]);
System.out.println("converted to array");
for (int i = 0; i < arrDays.length; i++) {
System.out.println(arrDays[i]);
}
}
}
So to convert ArrayList of any class into array use following code. Convert T into the class whose arrays you want to create.
OUTPUT
converted to array
sunday
monday
tuesday
wednesday
thursday
friday
saturday
import java.util.ArrayList;
import java.util.List;
public class ListToArray {
public static void main(String[] args) {
List<String> listDays = new ArrayList<String>();
listDays.add("sunday");
listDays.add("monday");
listDays.add("tuesday");
listDays.add("wednesday");
listDays.add("thursday");
listDays.add("friday");
listDays.add("saturday");
String[] arrDays = listDays.toArray(new String[listDays.size()]);
System.out.println("converted to array");
for (int i = 0; i < arrDays.length; i++) {
System.out.println(arrDays[i]);
}
}
}
So to convert ArrayList of any class into array use following code. Convert T into the class whose arrays you want to create.
List<T> list =
new
ArrayList<T>();
T [] arr = list.toArray(
new
T[list.size()]);
OUTPUT
converted to array
sunday
monday
tuesday
wednesday
thursday
friday
saturday
No comments:
Post a Comment
Note: only a member of this blog may post a comment.