Java

Converting Lists to Arrays in Java

Both lists and arrays are used to store multiple values in a variable, but there are some differences between the lists and arrays. The list of ordered collections is stored in the list and the duplicate values can be stored in the list. The size of the list varies and it uses more memory than the array. An array variable can store a primitive data type only and the size of the array is fixed. A list variable can be converted into an array variable using multiple built-in functions of Java. The uses of different Java functions to convert a list into an array are shown in this tutorial.

Example 1: Convert a List of Strings Using toArray()

Create a Java file with the following code that converts a list of strings into an array using the toArray() method. Four values are inserted into the list using the add() method and prints all values of the list. The size of the list is counted using the size() method of the list. Next, an array of strings is declared with the size of the list, and the list is converted into the array using the toArray() method. A “for” loop is used to print all values of the array.

import java.util.ArrayList;

import java.util.List;

public class ConvertListToArray1 {

    public static void main(String[] args) {
       
        //Declare a List variable


        List<String> strList = new ArrayList<String>();

             //Add element to the List
        strList.add("Rose");
        strList.add("Lily");
        strList.add("Sunflower");
        strList.add("Water Lily");

             //Print the List values
             System.out.println("The List values are:\n" + strList);

             //Count the size of the List
             int size = strList.size();

             //Declare an array variable
             String [] strArray = new String [size];

             //Convert the List into the Array
             strList.toArray(strArray);

             //Print the array values
             System.out.println("\nThe array values are: ");


             for (int i = 0 ; i < strArray.length ; i++)

             {
                   System.out.println(strArray[i]);
             }
    }


}

Output:

The following output appears after executing the code. Here, the list of values are printed in a line as a list and each value of the array is printed in each line:

Example 2: Convert a List of Numbers Using toArray()

Create a Java file with the following code that converts a list of numbers into an array using the toArray() method. Four integer numbers are inserted into the list using the add() method and each value of the list is printed using a “for” loop. Next, the length of the list is counted and converted into an array using the toArray() method like in the previous example. The array values are printed in the output by a “for” loop.

import java.util.ArrayList;

import java.util.List;

public class ConvertListToArray2 {

    public static void main(String[] args) {
       
        //Declare a List variable


        List<Integer> intList = new ArrayList<Integer>();

 

             //Add element to the List
        intList.add(89);
        intList.add(23);
        intList.add(75);
        intList.add(12);

             //Print the List values
             System.out.println("The List values are:");
             for(int val : intList)
             {
                  System.out.println(val);
             }

             //Count the size of the List
             int size = intList.size();

             //Declare an array variable
             Integer [] intArray = new Integer [size];

             //Convert the List into the Array
             intList.toArray(intArray);

             //Print the array values
             System.out.println("\nThe array values are: ");
             for(int val : intArray)
             {
                 System.out.println(val);
             }
    }


}

Output:

The following output appears after executing the code. Here, each value of both list and array is printed in each line:

Example 3: Using the Get() Method

Create a Java file with the following code that converts a list of numbers into an array using the get() method. Get() is a built-in method of Java list to read each value of the list based on the index. A list of integers is defined in the code and three numbers are inserted into the list. Next, the get() method is used to print the list values by adding a space and inserting the list values in an array. The “for” loop is used to print both list and array values in the output.

import java.util.ArrayList;

import java.util.List;

public class ConvertListToArray3 {

    public static void main(String[] args) {
       
        //Declare a List variable


        List<Integer> intList = new ArrayList<Integer>();

 

             //Add element to the List
        intList.add(7);
        intList.add(2);
        intList.add(9);

             //Print the List values
             System.out.println("The List values are:");

for(int i= 0; i< intList.size(); i++)

             {
                   System.out.print(intList.get(i) + " ");
             }

             //Count the size of the List
             int size = intList.size();

             //Declare an array variable
             Integer [] intArray = new Integer [size];

             //Convert the List into the Array


             for (int i = 0; i < size; i++)

             {

                intArray[i] = intList.get(i);
             }

             //Print the array values
             System.out.println("\nThe array values are: ");

             for (int i = 0 ; i < intArray.length ; i++)

             {
                   System.out.print(intArray[i] + " ");
             }
       }

}

Output:

The following output appears after executing the code. Here, all values of both list and array are printed in a line:

Conclusion

The methods of converting a list of numbers or strings into an array using two built-in functions of Java are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.