Home >Java >javaTutorial >Exploring the definition and common usage of Java arrays
In-depth understanding of the definition and common usage of Java arrays requires specific code examples
1. Definition and declaration of Java arrays
In Java , an array is a container that can store multiple elements of the same type. To define an array, you need to specify the type and length of the array. The syntax is as follows:
<数据类型>[] <数组名> = new <数据类型>[<长度>];
For example, define an integer array and set the length to 5:
int[] numbers = new int[5];
2. Common usage of arrays
Elements in the array can be accessed by index. The index starts from 0. Use the index value within square brackets to get the element. For example, get the first element in the array numbers:
int firstNumber = numbers[0];
The elements of the array can be modified by index. For example, change the third element in the array numbers to 10:
numbers[2] = 10;
You can use a for loop or enhanced for loop to traverse the elements in the array . For example, use a for loop to traverse all elements in the array numbers:
for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); }
Use an enhanced for loop to traverse all elements in the array numbers:
for (int number : numbers) { System.out.println(number); }
You can use the length keyword to get the length of the array. For example, get the length of the array numbers:
int length = numbers.length;
Java also supports multidimensional arrays, that is, the elements of the array can be arrays. For example, define a two-dimensional integer array:
int[][] matrix = new int[3][3];
Accessing the elements of the two-dimensional array requires the use of two indices. For example, get the elements of the second row and third column in the two-dimensional array matrix:
int element = matrix[1][2];
While defining the array, you can directly Assign an initial value to the element. For example, define an integer array with known element values:
int[] numbers = {1, 2, 3, 4, 5};
You can also use a loop to assign an initial value to the array. For example, define an integer array with a length of 10 and assign it a continuous integer from 1 to 10:
int[] numbers = new int[10]; for (int i = 0; i < numbers.length; i++) { numbers[i] = i + 1; }
You can use the Arrays class copyOf method to copy the array. For example, copy the array numbers to the new array newNumbers:
int[] newNumbers = Arrays.copyOf(numbers, numbers.length);
You can specify the length of the copy. For example, copy only the first 3 elements of the array numbers:
int[] newNumbers = Arrays.copyOf(numbers, 3);
3. Sample code
The following is a complete sample code that demonstrates the array definition and common usage mentioned above:
import java.util.Arrays; public class ArrayExample { public static void main(String[] args) { int[] numbers = new int[5]; // 修改数组元素 numbers[2] = 10; // 遍历数组 System.out.println("遍历数组:"); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } // 获取数组长度 int length = numbers.length; System.out.println("数组长度:" + length); // 定义并初始化数组 int[] newNumbers = {1, 2, 3, 4, 5}; // 数组拷贝 int[] copiedNumbers = Arrays.copyOf(newNumbers, newNumbers.length); // 多维数组 int[][] matrix = new int[3][3]; matrix[1][2] = 5; int element = matrix[1][2]; System.out.println("二维数组元素:" + element); } }
Through the above sample code, we can have a deeper understanding of the definition and common usage of Java arrays. After mastering the basic operations of arrays, we can apply arrays more flexibly to solve practical problems.
The above is the detailed content of Exploring the definition and common usage of Java arrays. For more information, please follow other related articles on the PHP Chinese website!