Convert ArrayList to An Array | Java Programs

Output:

*** Convert ArrayList to Array ***
ArrayList: 
[Alee, Ahmed, Kolachi, Mohammad, Umar, Pahore]

Converted to Array : 
array[0] = Alee
array[1] = Ahmed
array[2] = Kolachi
array[3] = Mohammad
array[4] = Umar
array[5] = Pahore

Click Here For Java Online Compiler

Solution:

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

public class ConvertArrayListToArray {

    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Alee");
        arrayList.add("Ahmed");
        arrayList.add("Kolachi");
        arrayList.add("Mohammad");
        arrayList.add("Umar");
        arrayList.add("Pahore");

        String[] array = new String[arrayList.size()];
        arrayList.toArray(array);
        System.out.println("*** Convert ArrayList to Array ***" + "\n");
        System.out.println("ArrayList: ");
        System.out.println(arrayList);
        System.out.println();
        System.out.println("Converted to Array : ");
        int x = 0;
        for (String s : array) {
            System.out.println("array[" + x + "] = " + s);
            x++;
        }

    }
}
Share This :

Related Post

avatar

nice article in your blog.thank you for sharing useful info.
visit
web programming tutorial
welookups

delete 29 August 2018 at 20:21



sentiment_satisfied Emoticon