import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; /** * Java 1.4+ Compatible * The following example code demonstrates converting an Array of Strings to an ArrayList of Strings */ public class Array2ArrayList { public static void main(String[] args) { // initialize array with some data String[] sa = new String[] { "A", "B", "C" }; // convert array to ArrayList ArrayList al = new ArrayList(Arrays.asList(sa)); // iterate over each element in ArrayList Iterator iterator = al.iterator(); while (iterator.hasNext()) { // Print element to console System.out.println((String) iterator.next()); } } }
Here is the output of the example code:
A B C
No comments:
Post a Comment