In Java, array is a set of elements with the same data type, and consecutive addresses on the memory. The array has the fixed number of elements and you can not change its size.
The elements of an array are marked with a index, starting at index 0. You can access its elements through index.
Let's see, a array with 5 elements, int type.
In addition to one-dimensional arrays, you sometimes have to work with two-dimensional or multiple-dimensional arrays. For a two-dimensional array, its elements will be marked with two indexes such as row index and column index. Below is an image of a two- dimensional array.
How to declare an array in Java.
Let's look at an example:
An illustrative example to use a for loop to assign values to the elements:
3- One-dimensional array and lặp for-each loop
Java 5 provides you with a for-each loop that allows you to traverse all elements of an array without using an index variable.
Running the example:
4- The Utility methods for One-Dimensional Array
Note: You need to have knowledge of class and methods before seeing this section. Otherwise you can temporarily ignore it.
The Java provides you with some static methods for arrays, such as sorting arrays, assigning values to all array elements, searching, comparing arrays, etc. These methods are defined in the Arrays class.
Below are some really useful, static methods defined in the Arrays class. These methods apply to the arrays of elements with byte, char, double, float, long, int, short, boolean types.
The following example uses some methods of the Arrays class.
Very often, you have to work with an array, usually with a one-dimensional array. However, in case you want to work with tabular data, with a lot of columns and rows, two-dimensional arrays should be used. For example, two-dimensional array will help you store information on chess-mans on a chessboard.
The rows and columns of two-dimensional array are marked with indices 0, 1, 2,.... To access an element of a two- dimensional array, you need to access by two indices: row index and column index.
length is a property of array. In case of two-dimensional array, this property is number of rows of the array.
You have three ways to declare a two-dimensional array.
If you declare an array of primitive type (byte
), the elements are not assigned values, but default values.
- Default value 0 corresponds to the types such as byte, double, float, long, int, short.
- Default value false corresponds to the boolean type.
- Default value '\u0000' (character null) corresponds to the char type.
Conversely, if you declare an array of reference type and if one element of the array is not assigned a value, it will be the null default value.
The two-dimensional array is actually an array of arrays.
In the Java, two-dimensional array is an array of arrays, therefore, you can declare a two- dimensional array simply by assigning the number of rows, without specifying the number of columns. Because the two-dimensional array is an "array of arrays", the length property of the two-dimensional array returns the number of rows in the array.