java--Array

Source: Internet
Author: User
Tags null null

There are a number of other ways in Java that can be used to hold objects, but the biggest difference between arrays and other containers is:

    • Efficiency

Arrays in Java are the most efficient way to store and randomly access an object's reference sequence.

    • Type

Arrays can hold basic types, but containers before using generics cannot. Before generics are used, other containers treat objects as if they were root class object handling. When an array is created, it is determined to hold a specific type

    • Size

The size of the array is determined when it is created, but the size of the container is not fixed. Like ArrayList. By creating a new instance and then moving all references in the old instance to the new instance, you can allocate more space.

An array is the first level object

Regardless of the type of array used, the array identifier is actually a reference to a real object created in the heap.

Arrays can be divided into arrays of objects and arrays of types, which are almost identical in use, with the only difference being that the object array holds the reference, and the base type directly holds the value of the base type.

Importjava.util.Arrays;classfruit{Static LongCounter = 0; Final Longid = counter++;    String name;  PublicString toString () {return"Fruit" +ID; }} Public classCLASS15 { Public Static voidMain (string[] args) {fruit[] f=NewFruit[5];        System.out.println (Arrays.tostring (f));  for(inti = 0; i < f.length; i++) {F[i]=Newfruit ();        } System.out.println (Arrays.tostring (f)); int[] i =New int[5];            System.out.println (arrays.tostring (i)); }}

Output:

[nullnull NULL null-NULL][fruit0, fruit1, Fruit2, Fruit3, fruit4][ 0, 0, 0, 0, 0]

When the new object is an array of objects, all the location references are automatically initialized to null and no objects exist, but the base type array is stored in the default value of the base type.

Second, return an array

If you want to write a function that returns an array, the C and C + + functions return a pointer to the array.

In Java, you can directly return an array, which is the responsibility of the garbage collector, and it will persist when you need it, and it will be cleaned up when you are done with it.

classfruit{Static LongCounter = 0; Final Longid = counter++;    String name;  PublicString toString () {return"Fruit" +ID; }    StaticFruit[] Getfruit (inti) {fruit[] results=NewFruit[i];  for(intj = 0; J < I; J + +) {Results[j]=Newfruit (); }        returnresults; }} Public classCLASS15 { Public Static voidMain (string[] args) {fruit[] f= Fruit.getfruit (10);  for(intj = 0; J < F.length; J + +) {System.out.print (f[j].tostring ()+ ","); }            }}

Output:

Fruit0,fruit1,fruit2,fruit3,fruit4,fruit5,fruit6,fruit7,fruit8,fruit9,

Three, multidimensional arrays.

 Public classCLASS15 { Public Static voidMain (string[] args) {int[] A = {                {The},                {4,5,6},        };        System.out.println (Arrays.deeptostring (a)); int[] B =New int[2] [3];        System.out.println (arrays.deeptostring (b)); int[][][] C =New int[2] [3] [4];            System.out.println (Arrays.deeptostring (c)); }}

Arrays.deeptostring () can convert a multidimensional array into multiple strings.

Output:

[[1, 2, 3], [4, 5, 6]][[0, 0, 0], [0, 0, 0]][[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

You can see int[][] b = new Int[x][y], which is the matrix of x row y columns, int[][][] c = new Int[x][y][z] is the matrix of x y-row Z-Columns.

An array can have any length (called a rough array) from each vector that makes up the matrix.

 Public classCLASS15 { Public Static voidMain (string[] args) {Random rand=NewRandom (47); int[][][] A =New int[Rand.nextint (5)][][];  for(inti = 0; i < a.length; i++) {A[i]=New int[Rand.nextint (5)][];  for(intj = 0; J < A[i].length; J + +) {A[i][j]=New int[Rand.nextint (5)];    }} System.out.println (Arrays.deeptostring (a)); }}

Output:

[[], [[0], [0], [0, 0, 0, 0]], [[], [0, 0], [0, 0]]

Iv. some practical methods in arrays

    • Equals () is used to compare the equality of two arrays (Deepequals () for multidimensional arrays),
    • Fill () is used to populate the array,
    • Sort () for array sorting,
    • BinarySearch () is used to find elements in an already sorted array,
    • ToString () is used to generate a string representation of an array, and hashcode is used to generate the hash code for the array.
 Public classCLASS15 { Public Static voidMain (string[] args) {Random rand=NewRandom (); int[] i =New int[5]; Arrays.fill (i,1); System.out.println ("I" +arrays.tostring (i)); int[] J =New int[7]; Arrays.fill (J,2); System.out.println ("J" +arrays.tostring (j)); System.out.println ("I, j contrast results:" +Arrays.equals (i, j)); System.arraycopy (i,0, J, 0, i.length); System.out.println ("J" +arrays.tostring (j));  for(intk = 0; K < J.length; k++) {J[k]= Rand.nextint (10); } System.out.println ("J" +arrays.tostring (j));        Arrays.sort (j); System.out.println ("J After sorting" +arrays.tostring (j)); intloc = Arrays.binarysearch (j, 1); System.out.println ("j[" + loc + "]=1"); }}

Output:

J[2, 2, 2, 2, 2, 2, 2]i, J comparison results:falsej[1, 1, 1, 1, 1, 2, 2]j[5, 1, 6, 3, 7, 3, 9]j sort After [1, 3, 3, 5, 6, 7, 9]j[0]=1

java--Array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.