Java pass list of Strings to method

You can send any data types of argument to a function [string, number, list, dictionary etc.], and it will be treated as the same data type inside the function.

E.g. if you send a List as an argument, it will still be a List when it reaches the function:

def my_function[food]:  for x in food:    print[x]fruits = ["apple", "banana", "cherry"] my_function[fruits]

Try it Yourself »


This post will discuss how to convert a list of strings to a string array in Java.

1. Using List.toArray[] method

We can use the toArray[T[]] method to copy the list into a newly allocated string array. We can either pass a string array as an argument to the toArray[] method or pass an empty string type array. If an empty array is passed, JVM will allocate memory for the string array. Please note that if no argument is passed to toArray[], it will return an Object array.

    // convert a list of strings to a string array in Java

    public static void main[String[] args]

        List list = Arrays.asList["NYC", "New Delhi"];

        String[] array = list.toArray[new String[0]];

        System.out.println[Arrays.toString[array]];

Download  Run Code

Output:
[NYC, New Delhi]

2. Using Java 8

Java 8 provides another simple approach to convert a list of strings to a string array. Following are the steps:

The following program demonstrates it:

    // convert a list of strings to a string array in Java 8 and above

    public static void main[String[] args]

        List list = Arrays.asList["NYC", "New Delhi"];

        String[] array = list.stream[]

        System.out.println[Arrays.toString[array]];

Download  Run Code

Output:
[NYC, New Delhi]

3. Using Arrays.copyOf[] method

We can also use Arrays.copyOf[] to copy the specified array to an array of the specified type.

    // convert a list of strings to a string array in Java

    public static void main[String[] args]

        List list = Arrays.asList["NYC", "New Delhi"];

        String[] array = Arrays.copyOf[list.toArray[], list.size[],

        System.out.println[Arrays.toString[array]];

Download  Run Code

Output:
[NYC, New Delhi]

4. Using System.arraycopy[] method

We can use System.arraycopy[] that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

    // convert a list of strings to a string array in Java

    public static void main[String[] args]

        List list = Arrays.asList["NYC", "New Delhi"];

        String[] array = new String[list.size[]];

        System.arraycopy[list.toArray[], 0, array, 0, list.size[]];

        System.out.println[Arrays.toString[array]];

Download  Run Code

Output:
[NYC, New Delhi]

5. Naive solution

This post is incomplete without re-inventing the wheels. A naive approach uses regular for-loop to iterate over the list of strings and simply copy elements from a list to string array.

    // convert a list of strings to a string array in Java

    public static void main[String[] args]

        List list = Arrays.asList["NYC", "New Delhi"];

        // allocate memory for string array

        String[] array = new String[list.size[]];

        // copy elements from a list to a string array

        for [int i = 0; i

Chủ Đề