Java is an object-oriented language, and its arrays are no exception. In Java, arrays are a very basic yet powerful data structure that can be used to store multiple values of the same type. This article aims to help beginners understand and master the basic knowledge of Java arrays.
What is an array?
An array is a container that can hold a fixed number of elements of the same type. For example, you might have an integer array that can store 100 integers, or a string array that can store 50 strings, etc.
In Java, arrays are objects. Each array belongs to the class Array and has a fixed length (i.e., the number of elements it can hold). This means you cannot change the length of an array; once created, it is fixed.
How to declare an array?
To declare an array in Java, you need to specify the element type of the array and the name of the array. The format is as follows:
For example, to declare an integer array, we can write:
int[] numbers;
How to create an array?
Although we have declared an array, we have not actually created it. To create an array, we need to use the new operator and specify the length of the array. For example:
numbers = new int[10];
The above code will create an array that can hold 10 integers and assign it to the previously declared numbers variable. At this point, all elements of the array will be initialized to 0 (for integer arrays).
You can also create an array while declaring it:
int[] numbers = new int[10];
How to initialize an array?
You can assign values to an array while declaring it. This is called array initialization. For example:
int[] numbers = {1, 2, 3, 4, 5};
The above code will create an integer array with five elements and initialize these elements to 1, 2, 3, 4, 5 at the same time.
If you have already declared an array, you can use subscripts (also known as indexes) to access and modify each element of the array. In Java, the index of an array always starts from 0. For example:
numbers[0] = 10; // Assign 10 to the first element
numbers[1] = 20; // Assign 20 to the second element
How to access array elements?
Once you have initialized an array, you can access each of its elements through the index. For example:
int firstElement = numbers[0]; // Get the first element
System.out.println(firstElement); // Print the first element
How to traverse an array?
In Java, you can use a for loop or a foreach loop to traverse an array. For example:
Using a for loop:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Using a foreach loop:
for (int num : numbers) {
System.out.println(num);
}
Multidimensional arrays
Java supports multidimensional arrays, the most common of which is the two-dimensional array. A two-dimensional array is actually an array of arrays. The ways to declare, create, and initialize a two-dimensional array are as follows:
Declaration and creation:
int[][] matrix = new int[3][3]; // A 3x3 two-dimensional array
Initialization:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
You can use nested for loops to traverse a two-dimensional array:
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Arrays in Java are a very basic and powerful data structure, suitable for storing and processing large amounts of data. Although the length of a Java array cannot be changed after creation, Java provides other data structures such as ArrayList, which can dynamically add and remove elements. It is hoped that this article can help you understand and master arrays in Java. Next, I suggest you write some code yourself, create and manipulate arrays, which is the best way to learn.