ToArray () exports an array of type Object, whereas toArray[T[] a] exports an array of type specified.

1. ToArray () method

The toArray() method returns an array of all the elements in the List, and the array is of type Object[]. Note also that toArray() returns a new array object, and executing toArray() multiple times yields different array objects, and modifying one array does not affect the other array objects. And it doesn’t affect the value of the elements that the List itself originally stored.

private static class People{ String name; public People(String name){ this.name = name; } @Override public String toString() { return "People{" + "name='" + name + '\'' + '}'; }} // The People class is no longer listed here, as in the previous example. public static void main(String[] args) { List<People> list = new ArrayList<>(); List.add (new People(" new People ")); List.add (new People(" new People ")); Object[] objects1 = list.toArray(); Object[] objects2 = list.toArray(); System.out.println("objects1 == objects2 : "+(objects1 == objects2)); ((People) Objects1 [1]).name = "; System.out.println("show objects1: "+ Arrays.toString(objects1)); System.out.println("show objects2: "+ Arrays.toString(objects2)); System.out.println("show list: "+list); }

Output results:

Objects1 == objects2: false show objects1: [People{name= 'xiaomiao'}, People{name= 'xiaomiao'}] show objects2: [People {name = 'Ming'}, People {name = 'flowers'}] show list: [People {name =' Ming '}, People {name = 'flowers'}]

As you can see from the output of this example, making a change to the element object itself will change the contents of all arrays returned by toArray(), including the element class contents of the original list container. As we can see from this example, if the array returned by List.toArray () contains references to the original List objects, we simply create a new array to hold these references. We do not copy or copy the original List objects. ArrayList toArray()

public Object[] toArray() {
    return Arrays.copyOf(elementData, size);
}

The implementation in ArrayList calls the copyOf() method of the ArrayList utility class, which is related to the storage structure of the elements in the ArrayList class. The copyOf() methods of Arrays.copyof () are the same as the toArray() method of List, because toArray() is essentially the method that calls Arrays.copyof () directly.

2,ToArray [T [] a] method

The toArray(T[] a) method takes a generic parameter and returns an array of the specified type, but the generic type must be the parent of the element type in the list or itself.

@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
    if (a.length < size)
        // Make a new array of a's runtime type, but my contents:
        return (T[]) Arrays.copyOf(elementData, size, a.getClass());
    System.arraycopy(elementData, 0, a, 0, size);
    if (a.length > size)
        a[size] = null;
    return a;
}

If the length of array A is smaller than the number of list elements, then we will directly call the copyOf() method of Arrays.Copyof () to make a copyOf Arrays.Arrays.Arrays.Arrays.Arrays. CopyOf (elementData, Size, a.getClass()), which is taken by the element type, is the last argument of Arrays. The toArray() method implements Array.copyOf (elementData, size).

List length = null; List length = null; List length = null; List length = null; List length = null; List length = null; The first call to System.arrayCopy (elementData, 0, a, 0, size) copies the elements from the array of elements in the ArrayList into the A object. I won’t go into detail on System.arrayCopy ().

If (a[size] = null); if (a[size] = null); if (a[size] = null); The purpose of this setting is that when the toArray(T[] a) method caller detects null in the returned array, he/she knows that there is no list object left.

conclusion

The toArray() method of the List interface simply calls Array.copyOf (elementData, size), putting references to the elements in the List into a new generated array.

The toArray(T[] a) method of the Arrays interface returns an array of the specified type (must be a parent of the List element type or itself). If the number of items in the array is smaller than the number of items in the List, then the copyOf() method of the Arrays will be called. ArrayCopy () calls System.arrayCopy () to store a reference to the list in the array. If there is still space left in the array, then a[size] is null. This null value allows the caller of the toArray(T[] a) method to determine that there is no list element after the null.