Home >Java >javaTutorial >Master the in-depth understanding of the definition and operation skills of Java arrays
In-depth understanding of the definition and operation skills of Java arrays
Arrays in Java are a common data structure that can store multiple elements of the same type and have Fixed size. In this article, we will delve into the definition and manipulation techniques of Java arrays, and provide specific code examples.
Definition of array
Arrays can be defined in Java in the following ways:
数据类型[] 数组名 = new 数据类型[数组长度];
or:
数据类型[] 数组名 = {元素1, 元素2, ...};
The data type can be basic Data type, which can also be a reference type.
Array access
The elements of the array can be accessed through the index. The index starts from 0 and increases in sequence. For example, to access the first element of the array, you can use the following code:
数组名[0]
Example:
int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers[0]); // 输出:1
Traverse the array
You can use a for loop or a foreach loop Traverse the array. The following is an example of using a for loop to traverse an array:
int[] numbers = {1, 2, 3, 4, 5}; for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
An example of using a foreach loop to traverse an array:
int[] numbers = {1, 2, 3, 4, 5}; for (int number : numbers) { System.out.println(number); }
The length of the array
You can use the array Name.length
Get the length of the array. For example:
int[] numbers = {1, 2, 3, 4, 5}; System.out.println(numbers.length); // 输出:5
Initialization of arrays
Arrays in Java are automatically initialized for basic data types, and are initialized to null for reference types. For example:
int[] numbers = new int[5]; System.out.println(numbers[0]); // 输出:0 String[] names = new String[3]; System.out.println(names[0]); // 输出:null
Copy of array
You can use the System.arraycopy()
method or the Arrays.copyOf()
method to copy an array Copy an array into another array. Examples are as follows:
int[] source = {1, 2, 3, 4, 5}; int[] target = new int[source.length]; System.arraycopy(source, 0, target, 0, source.length); System.out.println(Arrays.toString(target)); // 输出:[1, 2, 3, 4, 5] int[] source = {1, 2, 3, 4, 5}; int[] target = Arrays.copyOf(source, source.length); System.out.println(Arrays.toString(target)); // 输出:[1, 2, 3, 4, 5]
Sorting of arrays
You can use the Arrays.sort()
method to sort the array. For example:
int[] numbers = {5, 3, 1, 4, 2}; Arrays.sort(numbers); System.out.println(Arrays.toString(numbers)); // 输出:[1, 2, 3, 4, 5]
Multidimensional array
In addition to one-dimensional arrays, Java also supports multi-dimensional arrays. For example, a two-dimensional array can be defined as follows:
数据类型[][] 数组名 = new 数据类型[行数][列数];
Example:
int[][] matrix = new int[3][3]; matrix[0][0] = 1; matrix[0][1] = 2; matrix[0][2] = 3; // ...
The above is an in-depth understanding of Java array definition and operation techniques. By learning the definition and operation methods of arrays, we can process data more flexibly and efficiently. I hope the code examples provided in this article can help you better understand and use Java arrays.
The above is the detailed content of Master the in-depth understanding of the definition and operation skills of Java arrays. For more information, please follow other related articles on the PHP Chinese website!