How to convert an array to ArrayList in java?

Conversion of Array to ArrayList using Arrays.asList().


								
package com.mindclues;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ArrayListExample {

    public static void main(String[] args) {

	  /* Array Declaration and initialization*/
	  String charArr[]={"A", "B", "C", "D"};

	  /*Array to ArrayList conversion*/
	  ArrayList charList= new ArrayList();

	  /*Adding new elements to the converted List*/
	  charList.add("E");
	  charList.add("F");

	  /*Final ArrayList content display using for*/
          System.out.println("List before add array");
	  for (String str: charList)
	  {
		System.out.println(str);
       	  }
          //Add array To ArrayList
          charList= new ArrayList(Arrays.asList(charArr));
          System.out.println(" ");
          System.out.println("List after add array");
	  for (String str: charList)
	  {
		System.out.println(str);
       	  }
      }
}
							

								
List before add array
E
F
 
List after add array
A
B
C
D
							

Related Articles

post a comment