Common methods of Arrays and Collections

Arrays is a tool class for arrays. It provides many static methods for manipulating arrays, and it also provides a conversion method to collections. Collections is a tool class for collections. It also provides static methods that need to be collected internally, as well as a method to convert to arrays.

1. Arrays

This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory, which can treat arrays as lists. If the specified array reference is empty, the methods in this class will all throw a NullPointerException, unless otherwise stated.
1.1 The array cut method copyOfRange()
copies the specified range of the specified array to a new array. The initial exponent of the range (from) must be between zero and original.length, inclusive. The value of original[from] is placed in the original element of the copy (unless from == original.length or from == to ). The value of the subsequent element in the original array will be placed in the subsequent element in the copy. The final index of the range that must be greater than or equal to from (to) may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length-from. The length of the returned array will be to-from.

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)

1.2 Array to List
public static List asList(T... a) returns a fixed-size list supported by the specified array. (Change the returned list to "write array".) This method acts as a bridge between array-based and collection-based APIs, combined with Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain a few elements:

  List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 
2. Collections

This class is only composed by static methods or returns a collection. It contains polymorphic algorithms that operate on collections, "wrappers", return new collections supported by the specified collection, and other possible and final ones. If the collection or class object provided to them is null, all methods of this class throw a NullPointerException.

Guess you like

Origin blog.csdn.net/Wangdiankun/article/details/107467427