we discussed to initialize arraylist again, now we do initialize
syntax
ArrayList<Type> obj = new ArrayList<Type>(Arrays.asList(Object o1, Object o2, Object o3,...so...on));
import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
public class Example2{
public static void main(String args[]) {
//first way
String[] arrItem = new String[]{"item1","Item2","Item3","Item4"};
ArrayList<String> arraysAsList = new ArrayList<String>(Arrays.asList(arrItem ));
System.out.println("initialize1 Array as List :"+arraysAsList);
//second way
ArrayList<String> obj = new ArrayList<String>(Arrays.asList("Orange", "Apple", "Coconut"));
System.out.println("Initialize 2 Array as list :"+obj);
}
}
// result :
// initialize1 Array as List :[item1, Item2, Item3, Item4]
// Initialize 2 Array as list :[Orange, Apple, Coconut]
EmoticonEmoticon