Convert Arraylist to array in Java

Simple code to convert Arraylist to array.

package codsstore;

import java.util.*;

/**
 * @author Sameera Jayasekara
 */
public class ToArray {

    public static void main(String[] args) {

        List<Integer> myList = new ArrayList<Integer>();
        myList.add(1);
        myList.add(2);
        myList.add(3);

        Integer[] myArray = new Integer[myList.size()];
        myArray = myList.toArray(myArray);

        for (int x : myArray) {
            System.out.println(x);
        }
    }
}

1 comment: