Java ArrayList toArray

The toArray() method that both array collections implement does this for you. You typically use the method like this:

String[] getarray = new String[array1.size()];
getarray = array1.toArray(getarray);

The toArray() method here returns an array of type String[] containing all the elements from names in the correct sequence.

Program

0

 

 

 

Program Source

import java.util.ArrayList;

public class Javaapp {

    public static void main(String[] args) {
        
        ArrayList array1 = new ArrayList();
        array1.add("haj");
        array1.add("sof");
        array1.add("java");
        array1.add("tutorial");
        System.out.println("array1 datas = "+array1);
        
        String[] getarray = new String[array1.size()];
        getarray = array1.toArray(getarray);
        
        for(String str : getarray)
        {
            System.out.println(str);
        }
    }
}

Leave a Comment