toArray() Introduction
The toArray() method is a method provided in the List interface, which is used to implement the function of converting List objects into array objects.
There are two forms of the toArray() method, the method without parameters and the method with generics. Next, an example is given.
()
// toArray() source code public Object[] toArray() { return (elementData, size); }
This method cannot specify the type of the converting array, and the return value can only be an Object() array. Therefore, after obtaining the return value, you often need to do a type conversion to convert Object[] to the type we need. However, there are often problems when converting this part, as shown in the following example:
List<Integer> list = new ArrayList<>(); (1); (2); (3); Integer[] res = new Integer[()]; res = (Integer[])();
This code can pass syntax checking, but it will report a type conversion error at runtime, indicating that Object() cannot be simply converted to an array of other types.
: [; cannot be cast to [;
(T[] a)
// toArray(T[] a) source code public <T> T[] toArray(T[] a) { if ( < size) // Make a new array of a's runtime type, but my contents: return (T[]) (elementData, size, ()); (elementData, 0, a, 0, size); if ( > size) a[size] = null; return a; }
This method is more flexible than the previous method without parameters. It requires the user to provide a generic of the target object. After the array is converted, an array of the specified type will be returned, and there is no type conversion error. Examples of use:
List<String> list = new ArrayList<>(); ("1"); ("2"); ("3"); String[] res = new String[()];
This way you can convert the List object to String[].
Three ways to convert List to int[]
Two methods are given when introducing toArray() above. The second method with generics is more widely used. I recently used the second method and found some problems. The functions I want to implement are as follows:
Given a List object: List list = new ArrayList<>(); Convert it to an int[] array.
I implement the method with generics toArray(), and the code is as follows:
List<Integer> list = new ArrayList<>(); (1); (2); (3); int[] res = new int[()]; res = (res);
But in fact, the above code is wrong, because the generic parameter of toArray() cannot be an int, but can only be its wrapper class Integer, so the List object cannot be directly converted into a general-type array through toArray(). There are three specific conversion methods:
Method 1: cyclic assignment
Take out the elements in the List one by one and assign them to the corresponding position in the int[] array.
List<Integer> list = new ArrayList<>(); (1); (2); (3); int[] res = new int[()]; for(int i = 0; i < (); i++){ res[i] = list[i]; }
Although this method requires traversal, its idea is simple and generally there will be no errors.
Method 2: Conversion through generics
Taking the final target array as int[] as an example, converting from List to int[] array can be achieved with the help of Integer[] array. The code is as follows:
List<Integer> list = new ArrayList<>(); (1); (2); (3); Integer[] res = new Integer[()]; res = (res);
This way you can get an Integer[] array, and then use loop assignment to convert the Integer[] array.
Method 3: Use flow
Use the new method provided in Java 1.8 to implement it. The following are the mutual conversion codes of List, Integer[], and int[]
int[] data = {4, 5, 3, 6, 2, 5, 1}; // int[] to List<Integer> List<Integer> list1 = (data).boxed().collect(()); // (arr) can be replaced by (arr). // 1. Use to convert int[] to IntStream. // 2. Use boxed() in IntStream to pack. Convert IntStream to Stream<Integer>. // 3. Use Stream's collect() to convert Stream<T> to List<T>, so it is List<Integer>. // int[] to Integer[] Integer[] integers1 = (data).boxed().toArray(Integer[]::new); // The first two steps are on the same page, and this is Stream<Integer>. // Then use Stream's toArray and pass in the IntFunction<A[]> generator. // This will return the Integer array. // Otherwise, the default is Object[]. // List<Integer> to Integer[] Integer[] integers2 = (new Integer[0]); // Call toArray. Pass in parameter T[] a. This usage is currently recommended. // The same goes for List<String> to String[]. // List<Integer> to int[] int[] arr1 = ().mapToInt(Integer::valueOf).toArray(); // If you want to convert to int[] type, you have to convert to IntStream first. // Here we use mapToInt() to call Integer::valueOf to convert Stream<Integer> to IntStream // In IntStream, the default toArray() is converted to int[]. // Integer[] to int[] int[] arr2 = (integers1).mapToInt(Integer::valueOf).toArray(); //The same idea as above. First convert Integer[] to Stream<Integer>, and then to IntStream. // Integer[] to List<Integer> List<Integer> list2 = (integers1); // The easiest way. The same applies to String[] to List<String>. // Similarly <br> String[] strings1 = {"a", "b", "c"}; // String[] to List<String> List<String> list3 = (strings1); // List<String> to String[] String[] strings2 = (new String[0]);
Summarize
This is the end of this article about the usage of the toArray() method of List in Java. For more related content of the toArray() method of Java List, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!