0 votes
in JAVA by
How to convert list to array in java?

1 Answer

0 votes
by

How to convert list to array in java?

The best and easiest way to convert a List into an Array in Java is to use the .toArray() method.

Likewise, we can convert back a List to Array using the Arrays.asList() method.

The examples below show how to convert List of String and List of Integers to their Array equivalents.

Convert List to Array of String

import java.util.ArrayList;

import java.util.List;

public class ConvertArrayListToArray {

    public static void main(String[] args) {

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

        itemList.add(""item1"");

        itemList.add(""item2"");

        itemList.add(""item3"");

        String[] itemsArray = new String[itemList.size()];

        itemsArray = itemList.toArray(itemsArray);

        for(String s : itemsArray)

            System.out.println(s);

    }

}"

🔗Source: stackoverflow.com

🔗Source: Java Interview Questions and Answers

Related questions

0 votes
asked Oct 18, 2020 in JAVA by sharadyadav1986
0 votes
asked Mar 16, 2021 in JAVA by Robindeniel
...