List and arrays are two ways to store data in Java that you will use quite often. In projects where you need to store data without any sort of file handling, list and arrays let you store data throughout the execution of your program.
We’ll be using Class ArrayList that implements List Interface in Java interchangeably throughout this tutorial.
Difference Between List and Array
List | Array |
The size of a list can be changed | The size of an array can’t be changed |
You can’t store Primitive types in List | You can store Primitive types in Array |
You can use Generics with List | You can’t use Generics with Array |
Consumes more memory | Consumes less memory |
Converting List to Array with Library Function
List has a
toArray() method which directly converts the contents of any list into an array while retaining the placement of text in the Array as it was in the original list. Here is the algorithm/steps to convert a list to array in java using this built-in library function.
- Initialize an ArrayList.
- Add elements to the List through the list.add(data_type) method.
- Create an Array with the same size as the list.
- Convert the List into an Array by using the variable name of the array created in step 3 as an argument.
- Print the contents of the Array.
The code implementing these steps is given below.
import java.util.ArrayList;
public class convertListToArray {
public static void main(String[] args) {
//Converting List to Array With Library Function
//Declaration of Array List
ArrayList<String> sampleList = new ArrayList<String>();
//Adding Elements to Array List
sampleList.add("California");
sampleList.add("Texas");
sampleList.add("Illinois");
sampleList.add("Massachusetts");
sampleList.add("Florida");
sampleList.add("Virginia");
sampleList.add("Colorado");
//Printing the Array List
System.out.println("Elements of List: " + sampleList);
//Declaring Array with Equal Size to the List
String[]arr = new String [sampleList.size()];
//Converting List to Array
sampleList.toArray(arr);
//Printing the Array
System.out.print("Elements of Array: ");
for (int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
}
}
Converting List to Array Without Library Function
Sometimes, there may be a requirement to convert the list into an array without using built-in functions. The problem at hand may use certain restrictions in terms of library functions. This requirement is common for programming students that are encouraged to work without library functions to help them learn the inner workings of these functions before implementing them. In such cases, you can use the following steps to convert a list into an array in java without any library function.
- Initialize an ArrayList.
- Add elements to the List through the list.add(data_type) method.
- Create an Array with the same size as the list.
- Create a for loop that will iterate through each element of the ArrayList and pass it through to the Array[index] through the list.get(index) function.
- Print the contents of the Array to show you’re actually converting the list to an array.
The code for implementing these steps is given below.
import java.util.ArrayList;
public class converListToArray2 {
public static void main(String[] args) {
//Converting List to Array Without Library Functions
//Declaration of Array List
ArrayList<String> sampleList = new ArrayList<String>();
//Adding Elements to Array List
sampleList.add("California");
sampleList.add("Texas");
sampleList.add("Illinois");
sampleList.add("Massachusetts");
sampleList.add("Florida");
sampleList.add("Virginia");
sampleList.add("Colorado");
//Printing the Array List
System.out.println("Elements of List: " + sampleList);
//Declaring Array with Equal Size to the List
String[]arr = new String [sampleList.size()];
//Converting to Array
for (int i = 0 ; i < arr.length ; i++){
arr[i] = sampleList.get(i);
}
//Printing the Array
System.out.print("Elements of Array: ");
for (int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
}
}
Similarly, if you’re also wondering how to convert an Array into a List, you can do so by using the built-in
Array.asList() method or iterating through the array and storing values at each index in the List.
Pavlo Plynko
Java Developer at CodeGym
Before becoming a developer, Pavlo devoted 15 years to system administration, but he understood that he did not want to do this fo ...
[Read full bio]
GO TO FULL VERSION