Java basics: the length of the array, the copy of the array

This article will introduce the length of the array and the content of the array copy.

array length

In Java, the length of an array refers to the number of elements an array can hold. There is no predefined method to get the length of an array. We can get the length of an array in Java by using the array property length.

array.length : length is a final variable for arrays, with the help of length variables, we can get the size of the array.

Code example:

int[] a1 = new int[8];  
int lengthOfA1 = a1.length;

In the given code snippet, a1 is an array of type int with a capacity of 8 entries, lengthOfA1 is a variable that keeps track of the length of the array, we use the array name a1 followed by the dot. operator and the length attribute to determine the size of the array.

array copy

Array copying is often encountered in real development. The method of copying an array is generally divided into three situations: using the for loop, using the clone method, and using the arraycopy method.

for loop

When using a for loop to copy an array, you can use the length property of the array.

Let's go directly to the code:

String[] a1 = {"java", "Spring", "SpringBoot", "MySQL"}; 
String[] a2 = new String[a1.length];

for (int i = 0; i < a1.length; i++) {
    a2[i] = a1[i];
}

It can be seen from the code that a new string array a1 is created, and there are 4 strings in the array:

  • “java”
  • “Spring”
  • “SpringBoot”
  • “MySQL”

Then create a new array a2 with the same length as a1, traverse the a1 array, and assign the data at the same coordinates of a1 to the same coordinates of a2, thus realizing the copying between arrays.

clone method

Object.clone() inherits from the Object class in the array.

We use the clone method to copy an array of primitive types:

String[] a1 = {"java", "Spring", "SpringBoot", "MySQL"}; 
 
String[] a2 = a1.clone();

They have the same content after cloning, but they hold different references, so any changes in one will not affect the other.

On the other hand, if we use the same method to clone an array of non-primitive types, then the result will be different.

array copy method

In Java, the System class contains a method called arraycopy() to copy an array. This method is a better way to copy an array than the above two methods.

The arraycopy() method allows you to copy a specified portion of a source array to a destination array:

arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
  • src : the source array to copy
  • srcPos : the starting position (index) in the source array
  • dest : the destination array where elements will be copied from the source
  • destPos : the starting position (index) in the destination array
  • length : the number of elements to copy

Let's take an example:

String[] a1 = {"java", "Spring", "SpringBoot", "MySQL"}; 

String[] a2 = new String[4];

System.arraycopy(a1, 0, a2, 0, a1.length);

Summarize

This article introduces the length of arrays in Java and how to copy arrays. The recommended method is to use the arraycopy method.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132290788