Example 1 — Convert a List to a String Array
Convert a list of strings to an Object[] and to a String[], and print both.
import java.util.Arrays;
import java.util.LinkedList;
public class LinkedListToArrayDemo {
public static void main(String[] args) {
LinkedList<String> fruits = new LinkedList<>(Arrays.asList("fig", "kiwi", "lime"));
Object[] plain = fruits.toArray();
String[] typed = fruits.toArray(new String[0]);
System.out.println("Object[]: " + Arrays.toString(plain));
System.out.println("String[]: " + Arrays.toString(typed));
System.out.println("length: " + typed.length);
System.out.println("first upper: " + typed[0].toUpperCase());
}
} How It Works
- Two forms. The first call needs no argument and gives an
Object[]. The second takes an emptyStringarray to say which type is wanted. - Same elements, same order. Both arrays contain fig, kiwi and lime.
- The typed array can use String methods.
typed[0].toUpperCase()works without a cast, giving FIG. - The length matches the list. The array has three slots, so
typed.lengthis 3.

