Reprint Please specify the Source:Jiq ' s technical Blog
First, array initialization
In any case, when you want to instantiate an array object, you must specify its size.
You cannot specify the dimension of an array without providing a initializer!!!
The correct initialization methods are:
Mode 3:int[][] array =new int[3][5]; for (int I =0;i<array.length;i++) {for (int j=0;j<array[i].length;j++) {Array[i][i] = i*j;}}
second, the advantages of the array relative to the container
The array originally has three advantages: efficiency (the price paid is fixed size), type safety, and the ability to save basic types.
Before generics, the container treats the objects as if they were not of any specific type, but the array designation holds a specific type and can be checked at compile-time, but this advantage disappears since the introduction of the generics in 1.5. Since the introduction of the packaging mechanism, the container can also be easily applied to the basic type.
So, the advantage of array only remaining is "efficiency".
The principle of choice is that when you are programming with the most recent Java version, you should prefer containers instead of arrays, only to prove that performance has become a problem ( switching to arrays helps improve performance ) , you should refactor the program to an array.
Third, Array in Java is a class in
Regardless of which type of array is used, the array ID is just a reference to a real object in the heap.
(1) In the case of an array of objects, a reference to a series of objects is saved in the array reference.
(2) In the case of an array of primitive types, the values of a series of primitive types are stored in the array reference.
The array object has only one read-only member length, and the ' [] ' syntax is the only way to access the array object.
Four, Java if you want to return a set of values, use the array
C + + is not possible, only the pointer can be returned, although the life cycle of the control array will become difficult, but Java does not need to be an array of responsibility, as long as you need him, it will always exist, when you are finished, the garbage collector will clean it off.
Publicstring[] Getfruits () {...}
Five, rough array
Java supports rough arrays, that is, each vector that makes up the matrix has any length
Inta[][] = {{$}, {2, 4, 5}, {6, 4, 8, 9}};
Six, array + generic type
You cannot instantiate an array with a parameterized type, because the array must know the exact type it holds . But you can parameterize the type of the array itself.
|
type<orange>[] pp = new type<orange>[10]; |
//compile ErroR |
|
public static <T> t[] f (t[] args) {...} |
Ok |
Seven, Arrays class
|
Array.sfill (A, 11);
Arrays.fill (b, 2, 7, "Hello"); |
Fills an array with a single value and, if it is an object, copies the reference. |
|
|
Arrays.equals (); |
Compares two arrays for equality. Deepequals () is used for multidimensional arrays. This method overloads all base types and objects to compare two arrays. The equal condition is that the number of elements is equal, and the elements of the corresponding positions are equal. |
|
|
Class Type implements Comparable<>
Type[] A = new type[10];
Arrays.sort (a);
Class Mycomparator implements Comparator{}arrays.sort (A, New Mycomparator ()); |
Sorts the array. Any primitive type array can be sorted, or an array of objects can be sorted, as long as the object implements the comparable interface (containing the CompareTo method) or has an associated comparator. |
|
|
Arrays.binarysearch (); |
Finds an element in a sorted array. |
|
|
Arrays.hashcode (); |
Generates the hash code for the array. |
|
|
Arrays.aslist (); |
Receive any sequence or array as a parameter and convert to a list. |
|
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Basics: Arrays