Java arrays help to store a group of values using a single variable.  In this article, we will take a closer look at arrays, understand the types of arrays, and also touch upon the various array operations.

  1. Introduction to Arrays
  2. One Dimensional Array
  3. Multi-dimensional arrays
  4. Three Dimensional Arrays
  5. Conclusion

Java Arrays

Introduction to Arrays

As mentioned earlier, Java arrays help to refer to a group of related values using a single variable. For example, suppose you are querying a database table and you need to store the value of a particular column. Without arrays, you would need to create as many variables as there are records in the tables. This would not only make the code complex and hard to understand, but it would also require a lot of maintenance. With arrays, you can have a single variable that stores the values corresponding to all the records. So, this simplifies the code to a great extent.

The values in the array can be of any data type but all the values in the array should be of the same data type. Once an array is created, its size remains fixed and cannot be dynamically modified. Arrays can contain both primitive as well as reference types of data. Arrays can be one-dimensional or multi-dimensional.

One Dimensional Array

A one-dimensional array is simply a list of values referred to by a common name.

Declaring an array

An array declaration consists of the data type of the array, the name to be given to the array variable, and square brackets. Square brackets are used in an array declaration in order to distinguish an array from an ordinary variable.

Sample Code

int[] array1; //Line 1
double array2[]; //Line 2
  • Line 1 declares an array array1 of type int. Here, the square brackets are specified after the data type
  • Line 2 declares an array array2 of type double. Here, the square brackets are specified after the variable name.
  • Both the approaches are correct, however, Line 1 is the preferred approach

Creating an array

Just declaring an array as shown above does not allocate memory to it. The new keyword needs to be used followed by the desired size of the array. This actually creates the array in memory.

Sample Code

int[] array1 ; //Line 1
array1 = new int[10]; //Line 2
double array2[] = new double[20]; //Line 3
  • Line 1 declares an array array1 of type int
  • Line 2 actually creates array1 with size 10
  • Line 3 creates an array array2 of type double with size 20
  • Line 3 combines the array declaration with memory allocation

Adding/Accessing data to/from an array

Once an array is created, data can be added to it. The data from the array can also be accessed by specifying its position.  Each value within the array is known as an element in the array. The position of an element in the array is known as its index. An element can be inserted into an array or accessed from the array by specifying its index in square brackets. The array index begins at 0, so the first element is at position 0, the second element is at position 1, and so on.

Sample Code

int[] numbers = new int[3]; //Line 1
numbers[0] = 10; //Line 2
numbers[1] = 70; //Line 3
numbers[2] = 25; //Line 4
int numberFromArray = numbers[1]; //Line 5
System.out.println("Value at position 1:" + numberFromArray); //Line 6

 

  • Line 1 creates an array numbers of type int with size3
  • Line 2 assigns the value 10 to the first element, that is the element at position 0
  • Similarly, Lines 3-4 assign the values 70,25 to the elements at position 1,2
  • Line 5 assigns the value at position 1 to the variable numberFromArray
  • Line 6 prints the value of numberFromArray

Output:

Value at position 1:70

Initializing an array

Arrays can be initialized at the time they are declared. Instead of using the new keyword and specifying the size of the array, the values to be stored in the array need to be specified as a comma-separated list in curly brackets.

Sample Code

String[] fruits = {"apple","mango","banana"}; //Line 1
System.out.println("Fruit at position 2:" + fruits[2]); //Line 2
  • Line 1 declares an array called fruits with the specified values
  • Since 3 values are specified, Line 1 creates an array whose size is 3 and stores the values specified in the array
  • Line 2 prints the value at position 2

Output:

Fruit at position 2:banana

Determining the size of an array

The array object has a property called length. This returns the number of elements in the array and can be used to determine the size of the array.

Sample Code

String[] fruits = {"apple","mango","banana","cherry"}; //Line 1
int lengthOfArray = fruits.length;//Line 2
System.out.println("Length is:" + lengthOfArray);//Line 3
  • Line 1 declares an array called fruits and initializes it with some values
  • Line 2 assigns the length of the array to the variable lengthOfArray

Output:

Length is:4

Iterating Through An Array

Any of the Java loops can be used to iterate over an array. However, the for-each loop is preferred since it automatically retrieves the next element in the array and automatically increments the loop counter.

Sample Code

String[] fruits = {"apple","mango","banana","cherry"};//Line 1

for(String fruit:fruits) { //Line 2
System.out.println(fruit); //Line 3
}
  • Line 1 declares an array called fruits and initializes it with some values
  • Line 2 uses a for-each loop to iterate through the array
  • The for-each loop simply fetches the next element in the array and prints it.

Output:

apple
mango
banana
cherry

Multi-dimensional arrays

A multi-dimensional array is an array of arrays. Each value in a multi-dimensional array is an array by itself. Multi-dimensional arrays are used to store data in a tabular form.

Creating a Two Dimensional Array

A two-dimensional array is an array of one-dimensional arrays.

Sample Code

int[][] numbers = new int[5][9];
  • This code creates an array called numbers with dimensions 5,9
  • So this is an array having 5 one dimensional arrays.  Each one-dimensional array has 9 elements

Initializing/Accessing Data in a Two Dimensional Array

Just like a one-dimensional array, a two-dimensional array can be initialized when it is declared. Also, its elements can be accessed by specifying the position of both dimensions.

Sample Code

int[][] numbers = {
{10,20,30},
{40,50,60}
}; //Line 1

int valueInArray = numbers[0][2]; //Line 2
System.out.println("Value at Position 0,2:"+valueInArray); //Line 3
  • Line 1 creates an array called numbers with dimensions 2,3 and initializes it with the specified values
  • Line 2 accesses the value at position 0,2 and assigns it to the variable valueInArray

Output:

Value at Position 0,2:30

Three Dimensional Arrays

A three-dimensional array is an array of two-dimensional arrays. It can be created and used just like any other array.

Sample Code

int[][][] numbers = new int[5][4][6]; //Line 1
numbers[3][0][4] = 8; //Line 2
System.out.println("Value at Position 3,0,4:"+numbers[3][0][4]);//Line 3
  • Line 1 creates a 3-dimensional array called numbers with dimensions 5,4,6
  • Line 2 assigns the value 8 to the element at position 3,0,4

Output:

Value at Position 3,0,4:8

Conclusion

So, just to summarise, arrays help to group values of the same data type which can be referred to by a common name. The values in an array can be accessed by specifying their position. Arrays can have one or more dimensions.