Arrays is one of the commonly used Classes in Java. Stop gossiping and return to the truth. Let’s Talk Android!

Guys, we mentioned array types in the previous chapter, because arrays are class types, and we didn’t talk about classes, so we just skipped over them. However, now that we know all about classes, we’ll introduce Arrays and the common class Arrays in this chapter.

1. Array definition

Let’s take a look at the array type variable definition method, we will use pseudo code to demonstrate:

type array[] = new type[size]; 
Copy the code

Contrast this with the way we define class objects:

ClassName obj = new ClassName();
Copy the code

You can see how similar they are! In Fact, in Java, array type is a kind of type, which is most commonly used in its member variable: length. From this member variable, we can get the size of the array, which is the value of size in the pseudocode. In addition, there is another form for defining arrays, which we demonstrate in pseudocode:

type[] array = new type[size];
Copy the code

This form is recommended in Java because it makes it obvious that it is an array type. In contrast to the previous definition, we can see that the previous form is the C language array definition form, in fact, Java for the sake of the programmer’s custom reserved.

2. Initialize the array

After we define a variable of array type, we always initialize that variable. Again, we demonstrate array initialization in pseudocode.

type array[] = {val1,val2,... };Copy the code

This method is the same as the array initialization method in C language. The advantage of this method is that the size of the array is automatically specified and the members in the array are initialized. There is another way to initialize arrays:

type array[] = new type[size];
Copy the code

This is typical object-oriented initialization. It initializes the size of the array based on the value of size and initializes each element of the array to the default value of the type. For example, if type is int, its default value is 0, then an array of ints initialized this way will start each element of the array with a value of 0. Now let’s talk about size here, it could be a constant or a variable, or it could even be 0. This is different from C, where you can only use constants.

3. Arrays

Java also provides the Arrays class, which provides lots of static methods for manipulating Arrays. Let’s look at some of the common static methods.

Arrays.fill(type[] arg0, type arg1); // Fill the array with arg0 using arg1
Arrays.sort(type[] arg0);           // Sort the group members in ascending order by default.
Arrays.binarySearch(type[] arg0, type arg1); // Find a variable in an array member, arg1 in a member of array arg0
Copy the code

These static methods are overloaded and can operate on different types of arrays. For example, if we replace type with int, it can operate on an array of type int, and if we replace type with double, it can operate on an array of type double. Here type can be not only a primitive type, but also a class type.

With that said, let’s do it. The following is the program code, please refer to:

public class  ArrayExam{
    public static void main(String args[])
    {
        int [] array1 = {1.2.3.9.8.7};int array2[] = new int[6];

        System.out.println("arrar1 length:"+array1.length);
        System.out.println("arrar2 length:"+array2.length);

        System.out.print("array1 members: ");
        for(int i=0; i<array1.length; ++i)
            System.out.print(array1[i]+"");
        System.out.println();

        System.out.print("array2 members: ");
        for(int i=0; i<array1.length; ++i)
            System.out.print(array2[i]+"");
        System.out.println();

        Arrays.fill(array2, 6); 
        Arrays.sort(array1);

        System.out.print("array1 members: ");
        for(int i=0; i<array1.length; ++i)
            System.out.print(array1[i]+"");
        System.out.println();

        System.out.print("array2 members: ");
        for(int i=0; i<array1.length; ++i)
            System.out.print(array2[i]+"");
        System.out.println();

        if( 1== Arrays.binarySearch(array1,6) )
            System.out.println("6 is found in array1");
        else
            System.out.println("6 is not found in array1"); }}Copy the code

In the above program, we define an array of type int, and then use two methods to initialize the array and print out the value of each member of the array. We then use the static methods of the Arrays class to fill, sort, and find Arrays. The following is the running results of the program, please refer to:

arrar1 length:6
arrar2 length:6
array1 members: 1 2 3 9 8 7 
array2 members: 0 0 0 0 0 0 
array1 members: 1 2 3 7 8 9 
array2 members: 6 6 6 6 6 6 
6 is not found in array1
Copy the code

Finally, arrays can be copied using an equal sign or assigned to an array (a1= A2). We don’t recommend this, though, because it’s equivalent to adding a new reference to an array. The usual approach is to use the static method provided by the Arrays class: copyOf(Type [] a, int b). Example:

a1=Arrays.copyOf(a2,a2.length)
Copy the code

Copy the contents of array A2 to array A1. Although these two arrays have the same content, they are different arrays. Because they have their own separate memory space. You can either print out the memory address or use == for comparison, but I’ll leave it to you. In addition, this method can also be used to modify the length of the array. The trick is in the method’s second argument, which indicates the length of the returned array. The following example shows doubling the length of array A2.

a2=Arrays.copyOf(a2,a2.length*2)
Copy the code

Let’s hear more examples of Java Arrays!