* ** Array features
Differences between arrays and other containers: efficiency, type, and ability to save basic types. In Java, arrays are the most efficient storage and Random Access Object Reference sequences. Array is a simple linear sequence, which makes element access very fast. However, the cost is that the size of the array object is fixed and cannot be changed during its lifecycle. All containers of the array are better than those of the generic type, because: 1 can create an array to hold a certain type, and prevent insertion error types and improper extraction types through compiler check. 2 Arrays can hold arrays of the basic type and containers after the generic type are checked by type. The difference is that the [] access element is used with the array, while the container uses get set add and other methods. The two are very similar, the switch is easy, and the container has more functions. After the automatic packaging mechanism appears, containers can easily process basic types as well as arrays. The only advantage of arrays is efficiency. However, to solve general problems, more containers are used.
* ** Array is the first-level object array identifier, which is just a reference and points to a real object created in the heap. This object is used to save references pointing to other objects. This object can be implicitly created as part of the array initialization syntax or explicitly created using new. An object array stores references, while an array of basic types stores values of basic types.
* ** Each vector of a matrix in a multidimensional array can have any length (rough array ).
* ** Arrays class fill fills the array sort to sort the array arrays. Sort (comparable A, comparator B)
A is a class that implements the comparable interface. This interface only needs to override the compareto method. B is a sort model for classes that implement the comparator interface. arrays of basic types cannot be sorted using comparator. To sort arrays without repeated elements, you can use treeset (keep the sorting order) and linkedhashset (keep the insertion order)
Tostring: the string that generates the array indicates that deeptostring prints the multi-dimensional array hashcode to generate the hash code of the array. aslist accepts any sequence or array as the parameter, convert to list container equals to compare whether the array is equal deepequals multi-dimensional array comparison binarysearch to perform a quick search in the sorted array. If comparator is used for sorting, binarysearch must be performed using the same comparator.
* ** System. arraycopy array COPY method. Automatic packaging and unpacking are not performed. The two arrays must be of the same type. The basic type of copy is the value, and the object array copy is a reference, called shallow copy ).
* ** Arrays and generics cannot instantiate arrays with parameterized types. However, you can parameterize the types of arrays, or create non-generic arrays, and then transform them to 1.
class ClassParameter<T> {
public T[] f(T[] arg) {
return arg;
}
}
class MethodParameter {
public static <T> T[] f(T[] arg) {
return arg;
}
}
public class ParameterizedArrayType {
public static void main(String[] args) {
Integer[] ints = {
1, 2, 3, 4, 5
};
Double[] doubles = {
1.1, 2.2, 3.3, 4.4, 5.5
};
Integer[] ints2 = new ClassParameter<Integer>().f(ints);
Double[] doubles2 = new ClassParameter<Double>().f(doubles);
ints2 = MethodParameter.f(ints);
doubles2 = MethodParameter.f(doubles);
}
}
2
List<String>[] ls;
List[] la = new List[10];
ls = (List<String>[])la; // "Unchecked" warning
ls[0] = new ArrayList<String>();
Generics are effective at the boundaries of classes or methods. In a class or method, erasure usually makes generics unsuitable. For example, you cannot create a Generic Array.
public class ArrayOfGenericType<T> {
T[] array; // OK
@SuppressWarnings("unchecked")
public ArrayOfGenericType( int size) {
//! array = new T[size]; // Illegal
array = (T[]) new Object[size]; // "unchecked" Warning
}
// Illegal:
//! public <U> U[] makeArray() { return new U[10]; }
} ///:~