In this article, we will discuss how to convert ArrayList to Arrays in different ways
Conversion of ArrayList to Arrays[] :
- Using toArrays() method
- Using traditional way by iterating and converting one-by-one
1. Using toArrays(); method
Steps :
- Initialize ArrayList with sample values
- Convert ArrayList to String[] Arrays using toArrays(); method, by passing to ArrayList‘s size
- Print converted String[] Arrays in console using enhanced for-each loop
Method signature:
public Object toArrays();
ConvertArrayListToStringArrays.java
package in.bench.resources.collection;
import java.util.ArrayList;
public class ConvertArrayListToStringArrays {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Hollywood");
al.add("Bollywood");
al.add("Kollywood");
al.add("Tollywood");
al.add("Mollywood");
al.add("Sandalwood");
// creating String Arrays using ArrayList size
// conversion of ArrayList to String[] Arrays
String[] industry = al.toArray(new String[al.size()]);
System.out.println("Conversion of "
+ "ArrayList<String> to String[] Arrays\n");
// print Arrays values
for(String str : industry){
System.out.println(str);}
}
}
Output:
Conversion of ArrayList<String> to String[] Arrays
Hollywood
Bollywood
Kollywood
Tollywood
Mollywood
Sandalwood
2. Conversion of List into Arrays using traditional way
Steps :
- Initialize ArrayList with sample values
- create empty String[] Arrays of ArrayList size using size(); method of ArrayList
- Iterate through original ArrayList using traditional for-loop and assign each value of ArrayList to String[] Arrays
- Finally, print converted String[] Arrays to console using enhanced for-each loop
ConvertListIntoArrays.java
package in.bench.resources.collection;
import java.util.ArrayList;
public class ConvertListIntoArrays {
public static void main(String[] args) {
// creating ArrayList object of type String
ArrayList<String> al = new ArrayList<String>();
// adding elements to ArrayList object
al.add("Hollywood");
al.add("Bollywood");
al.add("Kollywood");
al.add("Tollywood");
al.add("Mollywood");
al.add("Sandalwood");
// create empty String[] Arrays of ArrayList size
String[] industry = new String[al.size()];
// conversion of ArrayList to String[] Arrays
// using traditional for-loop
for(int index=0; index < industry.length; index++){
// assign each element of AL to String[]
industry[index] = al.get(index);
}
System.out.println("Conversion of "
+ "ArrayList<String> to String[] Arrays\n");
// print Arrays values
for(String str : industry){
System.out.println(str);}
}
}
Output:
Conversion of ArrayList<String> to String[] Arrays
Hollywood
Bollywood
Kollywood
Tollywood
Mollywood
Sandalwood
Related Articles:
- Java – Conversion of List to Map
- Java – Conversion of Map to List
- Java – Conversion of Arrays to List
- Java – Conversion of List to Arrays
- Java 8 – Conversion of List to Map
- Java 8 – Conversion of Map to List
- Java 8 – Conversion of Arrays to List
- Java 8 – Conversion of List to Arrays
- Java 8 – How to convert HashMap to ArrayList ?
- Java – Conversion of ArrayList to Arrays in 2 ways
- Java – Conversion of Arrays to Vector
- Java – Conversion of Arrays to HashSet
- Java – Conversion of HashSet to Arrays
- Java – Conversion of LinkedList into Vector
- Java – Conversion of ArrayList into HashSet to remove duplicate elements
- Java – How to convert LinkedList to contain unique elements in ascending-order ?
- Java – Converting Ordered ArrayList into Sorted TreeSet
- Java – How to remove elements while iterating collection object ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
Happy Coding !!
Happy Learning !!