How to use Java language arrays
Java is an object-oriented programming language that provides extensive array support. An array is a collection of data elements, each of which has the same data type and can be accessed by an index value (subscript). In Java, an array is an object, so they need to be initialized with the keyword new. In this article, we will discuss the basic usage of arrays in Java.
In Java, an array needs to specify its type and length when it is declared. For example, the following code creates an array containing 5 integers:
int[] myArray = new int[5];
This statement will create an array named myArray and allocate storage space for 5 integers. Array length in Java is fixed, so we cannot change the size of the array at runtime. If you need to change array size, use Java collection classes.
Array elements can be accessed by index. In Java, the indexing of arrays starts from 0, so the index of the first element is 0. For example, to access the third element in the myArray array, you can use the following statement:
int thirdElement = myArray[2];
In this example, we assign the value of the third element of the myArray array to thirdElement.
In Java, you can use braces to initialize an array. For example, the following code creates an array of 5 integers and initializes them to 1, 2, 3, 4, and 5:
int[] myArray = {1, 2, 3, 4, 5};
You can also initialize when creating the array using the new keyword . For example:
int[] myArray = new int[]{1, 2, 3, 4, 5};
In this example, we use the new keyword to create an integer array named myArray and initialize it to 1, 2, 3, 4, and 5.
Arrays in Java can be used for various calculations. For example, the following code demonstrates how to add all the elements in an array:
int sum = 0; for(int i = 0; i < myArray.length; i++) { sum += myArray[i]; }
In this example, we use a for loop to iterate through the myArray array and add all the elements.
In Java, you can create multidimensional arrays. A multidimensional array is an array containing nested arrays, which can be two, three, or higher dimensions. For example, in the code below, we create a 2D array with 3 rows and 4 columns:
int[][] myArray = new int[3][4];
You can use two for loops to iterate through all elements of a 2D array. The following code demonstrates how to initialize a two-dimensional array and calculate the sum of each row:
int[][] myArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int[] rowSum = new int[3]; for(int i = 0; i < myArray.length; i++) { for(int j = 0; j < myArray[i].length; j++) { rowSum[i] += myArray[i][j]; } }
In this example, we initialize a 3x3 two-dimensional array myArray as {1, 2, 3}, {4, 5, 6}, {7, 8, 9} and use two for loops to calculate the sum of each row.
Summary
This article introduces the basic use of Java language arrays. They are collections of stored data elements, each of which can be accessed through an index. In Java, an array is an object, so they need to be initialized with the keyword new. In Java, arrays can be initialized using curly braces, and arrays can be used in various calculations, including multidimensional arrays. Now that you know the basics of Java arrays, you can start creating and manipulating your own arrays.
The above is the detailed content of How to use arrays in Java language. For more information, please follow other related articles on the PHP Chinese website!