How To - Convert Between ArrayLists & Arrays


Advertisements

Convert ArrayList to an Array

Here is the code for this method:

  ArrayList<String> sampleList = new ArrayList<String>();

  list.add("India");
  list.add("Switzerland");
  list.add("Italy");
  list.add("France");

  String[] countries = sampleList.toArray(new String[sampleList.size()]);
  

Converting Array to ArrayList

Here is the code for this method:

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

   ...

   String[] countries = {"India", "Switzerland", "Italy", "France"};
   ArrayList<String> list = new ArrayList(Arrays.asList(countries));


java_strings.htm

Advertisements