Java zero foundation entry series – array in Day10 Java

What is an array? As the name suggests, it is a combination of data, putting some numbers of the same type into a group.

So why use arrays? For example, when it is necessary to count the scores of the whole class, it is obviously inconvenient to name a variable to store the score information of 50 students in the class. Moreover, when making score statistics, such as total score, average score and variance, traversing the score information has become a big problem. At this time, you need to use the array.

Array can well solve these two problems. There is only one array name. You only need to use the array name and subscript to access the information of each element. During traversal, because the subscript is continuous, it is very convenient to access.

Next, let's formally introduce arrays.

Array is a collection used to store the same data type. You need to declare the array before using the array. The declaration method is: int [] a; Only variables are declared here, and they are not really initialized to arrays. You should use the new operator to create an array, int [] a = New Int [100]; An integer array with a size of 100 is declared here, that is, it can store up to 100 integers. It should be noted that the subscript of the array is 0-99 instead of 1-100. Therefore, the last number in the array is a [99] instead of a [100]. After the array is defined, you can use the circular structure to assign values.

int[] a = new int[100];for(int i = 0; i < 100; i++)
    a[i] = i;

For a new digital array, the elements in it will be initialized to 0. If it is a logical array, it will be initialized to false, and the object array will be initialized to null. When you create a string array, the elements in it will be null instead of an empty string. Therefore, if you want to initialize to an empty string, you need to do the above operations, Traverse each element to assign an empty string.

There is a simpler method for array traversal in Java, which is similar to the for each loop in c# and Java also has the same for each operation.

int element:a)
    System.out.println(element);

This code will print the elements in a in turn, and each element occupies one line. Each cycle here will temporarily store an element in array a in element. After processing, the next element will be stored in element. The advantage of this is that the subscript processing is omitted, so there is no problem of subscript crossing, and it looks more concise. Of course, if you need to use subscripts for some judgment or processing in the loop, you can only use the ordinary for loop. The two methods have their own advantages, which can be analyzed in detail.

There are several common ways to initialize arrays.

int[] a={1,2,3,4,5};//这里不需要使用newint[]{2,5,6};匿名数组a = <span style="color: rgba(0,6};

If you have studied C or C + +, here int [] a = New Int [100]; Equivalent to int * a = New Int [100] in C + +; That is, an array variable is a pointer, so when you copy an array variable to two array variables, they will point to the same array. For example, chestnuts:

int[] a;int[] b = {3,6};
a = b;
a[2] = 10;
System.out.println(b[2]);

Here 10 will be output, that is, the elements of the array pointed to by B have been changed. If you only want to copy the values of the elements in one array to another array, you need to use the copyof method of the arrays class. This method has two parameters, the first is the array to be copied, and the second is the length to be copied. Look at a chestnut:

package pers.frank.test;import java.util.Arrays;public class Test{static void main(String[] args) {[] a;};
        a = Arrays.copyOf(b,8);
        a[2] = 10;
        System.out.println("==B=="); i:b)
            System.out.println(i);
        System.out.println("==A==" i:a)
            System.out.println(i);
    }
}

In this way, the elements of array B and array a are output cyclically.

Here we set the second parameter to 8, and the length of array B is only 4. What happens? The answer is obvious. You'll fill the extra length with 0.

In the previous chestnuts, we often see the main method with string [] args in Java applications. This parameter table name. The main method receives a string array, that is, command-line parameters. For example, chestnuts:

if(args[0].equals("-h"))
            System.out.print("Hello,"else if (args[0].equals("-g"))
            System.out.print("Goodbye,1)">int i = 1; i < args.length; i++)
            System.out.print(" "+ args[i]);
        System.out.print("!");
    }
}

The operation results are as follows:

Next, let's talk about array sorting. There is a sort method in the arrays class, which is used to sort the elements of the array. It uses a quick sorting algorithm, which is an efficient algorithm. Here is a chestnut for demonstration.

int[] numbers = int[10];int i=0;i<10;i++){
            numbers[i] = (int)(Math.random()*10);
        }
        System.out.println("Before Sort:"for ( j:numbers){
            System.out.println(j);
        }
        Arrays.sort(numbers);
        System.out.println("After Sort:" j:numbers){
            System.out.println(j);
        }
    }
}

Finally, let's talk about multi-dimensional arrays. The previous arrays can only be regarded as one-dimensional arrays. Two dimensional arrays have rows and columns like excel tables. If you have learned about matrices, you should be familiar with two-dimensional arrays. It is also very simple to declare two-dimensional arrays in Java: int [] [] number; There is only one more pair [] than one-dimensional array. You also need to use new to define the array size. Of course, you can also use initialization methods similar to one-dimensional arrays.

int[][] numbers = {
  {1,3},  {4,  {7,8,9}
}

The access of array elements also needs to use two square brackets, such as numbers [1] [2]. The value here is the number 6. The first square bracket represents the row number and the second square bracket represents the column number, so here is the number of the second row and the third column. If you need to traverse a two-dimensional array, you need to use two for loops,

 i=0;i<numbers.length;i++( j=0;j<numbers[i].length;j++

As for higher dimensional arrays, they are generally not used. I won't introduce them here.

So far, this explanation is complete. Welcome to continue your attention!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>