In Java, the conversion between List and Array is a common operation. Since they are different data structures, Java provides some ways to convert between them. We will discuss the two situations from List to Array and Array to List separately.
1. List to array
Suppose you have an object of type `List` and you want to convert it to an array. You can use the toArray() method of the List` class.
(a) List to array (Object type)
The most common conversion is to convert a `List` into a normal object array `Object[]`, as follows:
import ; import ; public class ListToArrayExample { public static void main(String[] args) { // Create a List List<String> list = new ArrayList<>(); ("A"); ("B"); ("C"); // Convert List to array (Object type) Object[] array = (); // Output result for (Object obj : array) { (obj); } } }
In this example, `()` returns an array of type `Object[]`. At this time, the element type of the array is `Object`. If a more specific type is needed, you can cast it.
(b) List to array (specified type)
If you know that List is storing elements of a specific type (such as `String`), you can use the `toArray(T[] a)` method to specify the type of the array to avoid casting during conversion:
import ; import ; public class ListToArrayExample { public static void main(String[] args) { // Create a List List<String> list = new ArrayList<>(); ("A"); ("B"); ("C"); // Convert List to array (specified type) String[] array = (new String[0]); // Output result for (String str : array) { (str); } } }
In this example, (new String[0]) generates an array of type String[]. new String[0] represents the size of the array passed in. In fact, the size of the array will be automatically adjusted according to the number of elements in List.
(c) For more complex List types
If your List contains a custom type (such as a `Person` class), the conversion method is similar to the above:
import ; import ; class Person { String name; int age; Person(String name, int age) { = name; = age; } @Override public String toString() { return name + ", " + age; } } public class ListToArrayExample { public static void main(String[] args) { List<Person> list = new ArrayList<>(); (new Person("Alice", 30)); (new Person("Bob", 25)); // Convert List to an array of Person type Person[] array = (new Person[0]); // Output result for (Person person : array) { (person); } } }
2. Array to List
If you have an array and want to convert it to a `List`, you can do it with the `()` method.
(a) Array to List (basic data type)
For a simple array (such as `int[]` or `String[]`), you can do this:
import ; import ; public class ArrayToListExample { public static void main(String[] args) { // Create an array String[] array = {"A", "B", "C"}; // Convert array to List List<String> list = (array); // Output result for (String str : list) { (str); } } }
(b) Array to List (primitive type array)
Note that the `()` method does not return a `List` of type `List<Integer>` or `List<Double>` for primitive arrays (such as `int[]`, `double[]`, etc.), but returns a `List<int[]>` list of type `List<int[]>`. To avoid this, you need to wrap the original type array into the corresponding wrapper class array before using `()`.
import ; import ; public class ArrayToListExample { public static void main(String[] args) { // Create an int array int[] array = {1, 2, 3}; // Wrapping an int array into an Integer array Integer[] boxedArray = (array).boxed().toArray(Integer[]::new); // Convert Integer array to List List<Integer> list = (boxedArray); // Output result for (Integer num : list) { (num); } } }
(c) Array to List (custom object array)
If you have an array of custom types, the same applies to using `()`:
import ; import ; class Person { String name; int age; Person(String name, int age) { = name; = age; } @Override public String toString() { return name + ", " + age; } } public class ArrayToListExample { public static void main(String[] args) { // Create a Person-type array Person[] array = {new Person("Alice", 30), new Person("Bob", 25)}; // Convert array to List List<Person> list = (array); // Output result for (Person person : list) { (person); } } }
3. Things to note
(a) `()` limit
- The List created with `()` is a ** fixed-size List, which means you cannot add or delete elements to the List. If you try to do this, `UnsupportedOperationException` will be thrown.
For example:
List<String> list = ("A", "B", "C"); ("D"); // Thrown at runtime UnsupportedOperationException
If you need a modifyable List, you should wrap it in a new `ArrayList`:
List<String> list = new ArrayList<>(("A", "B", "C")); ("D"); // Can now be modified
(b) Type conversion via the `toArray()` method
If the type of List is not the type you want (for example, List stores an element of type `Object`), you can cast the type when performing a conversion. For example:
Object[] objArray = (); String[] strArray = (String[]) objArray; // Cast to String[]
(c) For primitive arrays
Java does not have a method to convert primitive arrays (such as `int[]`, `double[]`) to List directly. You can convert the original type array to a wrapper class array first, and then use the `()` method to convert it to a List. For example:
int[] array = {1, 2, 3}; Integer[] boxedArray = (array).boxed().toArray(Integer[]::new); List<Integer> list = (boxedArray);
Summarize
- List to array: Use the `toArray()` method. For typed List, the `toArray(T[] a)` method is recommended, which can avoid unnecessary type conversion.
- Array to List: Use the `()` method, but pay attention to the special case of primitive arrays (they will be converted to a List containing the array), and the returned List is of a fixed size and cannot be added or deleted.
This is the end of this article about some methods of converting lists and arrays in java. For more related contents of converting lists and arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!