CodesJava

Easy learning with example program codes

Arraylist to array in java

The toArray() method is used to convert arraylist to array in java.

Syntax:

arrayList.toArray(strArray);

Example:

package com.codesjava;
 
import java.util.ArrayList;
 
public class Test {
  public static void main(String args[]){
	ArrayList<String> arrayList = new ArrayList<String>();
	arrayList.add("Jai");
	arrayList.add("Mahesh");
	arrayList.add("Vivek");
	arrayList.add("Naren");
	arrayList.add("Hemant");
	arrayList.add("Vishal");
	System.out.println("Actual ArrayList:"+arrayList);
	String[] strArray = new String[arrayList.size()];
	arrayList.toArray(strArray);
        System.out.println("Created Array content:");
        for(String string:strArray){
            System.out.println(string);
        }
  }
}

Output

Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal]
Created Array content:
Jai
Mahesh
Vivek
Naren
Hemant
Vishal
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status