Java Program to convert String ArrayList to String Array
Converting String ArrayList into String array is very common programming task in Java. you often need to convert Array to Array List in Java and vice-versa. In this Java program, we will How to convert String ArrayList to String array. This is also a common programming exercise which is asked too many Java programmers in various Java related courses. It’s also worth noting that ArrayList in Java is internally backed by array and Array in Java are objects much like String which is also an Object in Java. In this Java program, we first create an ArrayList which stores name of months as String e.g. Jan, Feb, and Mar. Later we use ArrayList toArray() method to convert ArrayList into an array.
How to convert String ArrayList to Array in Java
How to convert String ArrayList to String Array in JavaHere is full code example of Java test program which convert String ArrayList to String Array in Java.In this Java program we first create an ArrayList which stores name of month in String format and then we convert that into an String array.
Java Program to convert an ArrayList of String to an Array
package arrayListEx;
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListtoArray {
public static void main(String args[]){
//ArrayList containing string objects
ArrayList<String> aListMonth = new ArrayList<String>();
aListMonth.add("Jan");
aListMonth.add("Feb");
aListMonth.add("mar");
/*
* To convert ArrayList containing String elements to String array, use
* Object[] toArray() method of ArrayList class.
*/
//First Step: convert ArrayList to an Object array.
Object[] objMnt = aListMonth.toArray();
//Second Step: convert Object array to String array
String[] strMnts = Arrays.copyOf(objMnt, objMnt.length, String[].class);
System.out.println("ArrayList converted to String array");
//print elements of String array
for(int i=0; i < strMnts.length; i++){
System.out.println(strMnts[i]);
}
}
}
Other examples :
=================
import java.util.ArrayList;
public class month {
public static void main(String args[]){
//ArrayList containing string objects
ArrayList aListMonth = new ArrayList();
aListMonth.add("Jan");
aListMonth.add("Feb");
aListMonth.add("Mar");
aListMonth.add("Apr");
aListMonth.add("Jun");
String strMnts[]=new String[aListMonth.size()];
strMnts=aListMonth.toArray(strMnts);
for(int i=0;i<strMnts.length;i++)
System.out.print(strMnts[i]+" ");
}
}
=====================================================================
The object array objMnt is of type Object[] and contains String references, that's why you can downcast a reference in the array. You can store String references in an Object[] because the class String is derived from the class Object and therefore you can substitute objects of type String for objects with type Object.
The Object[] itself cannot be downcasted to String[] because String[] is a derived class of Object[]. This means even tough the Object[] object contains references to Strings, the Object[] object itself is _NOT_ a String[] object.
But back to the problem: You could also convert the ArrayList of strings in an easier way:
String[] strMnts = aListMonth.toArray( new String[aListMonth.size()] );
This method exists (AFAIK) because of the exact reason mentioned above. By passing in the pre-allocated array of type String[], the actual return type of toArray is of type String[] and not of type Object[].
Converting String ArrayList into String array is very common programming task in Java. you often need to convert Array to Array List in Java and vice-versa. In this Java program, we will How to convert String ArrayList to String array. This is also a common programming exercise which is asked too many Java programmers in various Java related courses. It’s also worth noting that ArrayList in Java is internally backed by array and Array in Java are objects much like String which is also an Object in Java. In this Java program, we first create an ArrayList which stores name of months as String e.g. Jan, Feb, and Mar. Later we use ArrayList toArray() method to convert ArrayList into an array.
How to convert String ArrayList to Array in Java
How to convert String ArrayList to String Array in JavaHere is full code example of Java test program which convert String ArrayList to String Array in Java.In this Java program we first create an ArrayList which stores name of month in String format and then we convert that into an String array.
Java Program to convert an ArrayList of String to an Array
package arrayListEx;
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListtoArray {
public static void main(String args[]){
//ArrayList containing string objects
ArrayList<String> aListMonth = new ArrayList<String>();
aListMonth.add("Jan");
aListMonth.add("Feb");
aListMonth.add("mar");
/*
* To convert ArrayList containing String elements to String array, use
* Object[] toArray() method of ArrayList class.
*/
//First Step: convert ArrayList to an Object array.
Object[] objMnt = aListMonth.toArray();
//Second Step: convert Object array to String array
String[] strMnts = Arrays.copyOf(objMnt, objMnt.length, String[].class);
System.out.println("ArrayList converted to String array");
//print elements of String array
for(int i=0; i < strMnts.length; i++){
System.out.println(strMnts[i]);
}
}
}
Other examples :
=================
import java.util.ArrayList;
public class month {
public static void main(String args[]){
//ArrayList containing string objects
ArrayList aListMonth = new ArrayList();
aListMonth.add("Jan");
aListMonth.add("Feb");
aListMonth.add("Mar");
aListMonth.add("Apr");
aListMonth.add("Jun");
String strMnts[]=new String[aListMonth.size()];
strMnts=aListMonth.toArray(strMnts);
for(int i=0;i<strMnts.length;i++)
System.out.print(strMnts[i]+" ");
}
}
=====================================================================
The object array objMnt is of type Object[] and contains String references, that's why you can downcast a reference in the array. You can store String references in an Object[] because the class String is derived from the class Object and therefore you can substitute objects of type String for objects with type Object.
The Object[] itself cannot be downcasted to String[] because String[] is a derived class of Object[]. This means even tough the Object[] object contains references to Strings, the Object[] object itself is _NOT_ a String[] object.
But back to the problem: You could also convert the ArrayList of strings in an easier way:
String[] strMnts = aListMonth.toArray( new String[aListMonth.size()] );
This method exists (AFAIK) because of the exact reason mentioned above. By passing in the pre-allocated array of type String[], the actual return type of toArray is of type String[] and not of type Object[].