banner
jzman

jzman

Coding、思考、自觉。
github

Arrays in the Java series

This series will start with Java arrays. Arrays are data structures used to store a group of data with the same data type, and elements in the array can be accessed using indices.

  1. Array definition
  2. Essence of arrays
  3. Array copying

Array definition#

There are mainly two ways to define an array. One way is to specify the size of the array first and then assign values based on the indices of the array elements. Another way is to directly create an array and assign values to it, as shown below:

//1. Define an array with a size of 10
int[] arrayA = new int[10];
int arrayB[] = new int[10];
arrayA[0] = 1;
arrayB[1] = 2;
//2. Define an array and assign values
int[] arrayC = {1,2,3,4,5};

Essence of arrays#

In Java, arrays are actually classes. Therefore, two array variables can point to the same array. Consider the following code:

int[] arrayD = {1,1,1};
int[] arrayE = arrayD;
arrayD[0] = 2;
System.out.println(arrayE[0]);

Clearly, the result of executing the above code is 2. In the above code, the value of arrayD is assigned to arrayE. The essence of this is that both arrayD and arrayE point to the same array space. When a value of an element in arrayD is modified, the corresponding value in arrayE also changes accordingly, as shown in the following diagram:

image

Note: When an array is passed as a parameter to a method, it is equivalent to passing the reference of the array. Therefore, operations on the array in the method will also affect the original array. This is very important.

Array copying#

To obtain two arrays with the same values for each element, we can use the arraycopy() method provided by Java, as shown below:

int[] arrayD = {1,1,1};
int[] arrayF = new int[3];
// Copy the array
System.arraycopy(arrayD, 0, arrayF, 0, 3);
System.out.println(Arrays.toString(arrayF));

Clearly, after executing the above code, the values of arrayF are 1, 1, 1. If the values of the array elements in arrayD are indirectly modified, the values of arrayF will be 2, 1, 1. This result is obtained based on the context.

By the way, let's briefly explain the meanings of the parameters of the arraycopy method, as shown below:

/**
 * Copy an array
 * @param src: The original array
 * @param srcPos: The starting position in the original array to be copied
 * @param dest: The destination array
 * @param destPos: The starting position in the destination array
 * @param length: The length of the destination array
 */
public static void arraycopy​(Object src,
        int srcPos,
        Object dest,
        int destPos,
        int length) {
}

These are the main things to note about arrays. Of course, there are other APIs for manipulating arrays. The fact that assigning values between arrays affects the values of the original array is something I didn't notice before. That's all for today.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.