How to convert ArrayList to Array - Java @ Desk

Thursday, May 16, 2013

How to convert ArrayList to Array
















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

public class ArrayListToArray {
    public static void main(String args[]) {

        List strings = new ArrayList();
        strings.add("First Name");
        strings.add("Last Name");
        strings.add("Age");

        Object[] objArray = (Object[]) strings.toArray();
        String[] stringArray = Arrays.copyOf(objArray, objArray.length, String[].class);
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println("Value : " + stringArray[i]);
        }
    }
}






No comments:

Post a Comment