How to create a List from object Array?

How to create a List from object Array?

Previous Home Next

 

This example shows how to create List from an array object.

asList is a method of Array class which we used to create a List from an array object.
 asList method of Array class is used to create a List from an array object.

first we create a String Array.create an object of List as list the method asList is call through the Arrays and will pass it to the list object.and finally call the iterator in the list along with list object.

 

package Example;
import java .util.Arrays;
import java .util.List;
import java .util.Iterator;

public class ArraysList {

/**
* @param args
*/
public static void main(String[] args) {
//create a string array
String[]AString=new String[]{"aa","bb","cc","dd","ee","ff"};
List list=Arrays.asList(AString);
Iterator itr=list.iterator();
System.out.println("The string in the list are:");
while(itr.hasNext())
System.out.println(itr.next());


// TODO Auto-generated method stub

}


}

The string in the list are:
aa
bb
cc
dd
ee
ff

Previous Home Next