Site icon Automation Dojos

Java Tutorial #3 – Java Arrays

Table of Contents

Introduction

Arrays are used to store a group of values of the same data type. So a single variable can be used to refer to the group of values. Arrays can store data of any data type and can have one or more dimensions as explained below.

1. One-Dimensional Array

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

(a) Declaring an array

In order to create an array, you first need to declare a variable of the desired type corresponding to the array. The following code demonstrates this:

int myArray[]; // creates myArray to store int values

Notice that the name of the array is followed by square brackets. This indicates that we are trying to create an array. Since we have declared myArray to be of type int, each element in the array can only be of int data type.

(b) Allocating memory to the array

Although the code snippet above declares an array, you need to allocate memory to it using the new keyword as follows:

myArray = new int[10]; // creates array of size 10

Here, the keyword new is used to allocate memory. This is a special keyword that is used to allocate memory. This will be covered in detail when we take a closer look at classes.  Notice that the size (10 in this case) needs to be specified within square brackets as well. This indicates that the array is capable of storing 10 values.

The declaration and memory allocation can also be combined into a single statement as follows:

int myArray[] = new int[10]; //creates myArray with size 10
(c) Accessing an element in an array

Each value within the array is called as an element in the array. An element in an array can be accessed via its index. Index is simply an integer that specifies a position in the array. The index needs to be specified within square brackets too. Array index begins at 0, so the 1st element is at position 0, the 2nd at position 1 and so on.

Consider the following code snippet that assigns a value to a particular element:

myArray[3] = 5; // element at index 3, i.e.4th element will be assigned 5

Similarly, you can also access an element at a particular position using its index:

int value = myArray[2]; 
// value at index 2 i.e. 3rd position is assigned to the variable value
(d) Initializing an array

Arrays can also be initialized with some values when they are declared. You need to specify the values to be stored in the array within curly braces as a list of comma separated values. The following code demonstrates this:

int myArray[] = {2,4,6,8,10}; 
//creates new array of size 5 with specified values

Note that here the size is not specified in the square brackets explicitly; it is the same as the number of elements in the curly brackets.

(e) Obtaining the length of an array

There is a length property provided on the array object. This can be used to determine the number of elements in the array.  The following code demonstrates this:

int myArray[] = {2,4,6,8,10};
int size = myArray.length; // size will be 5

Since there are 5 values in the array myArray, size will be assigned the value 5.

2. Multi-Dimensional Arrays

Multidimensional arrays are basically arrays of arrays. They are used to store data in tabular form. Each additional index needs to be specified using additional square brackets.

(a) Creating a Two-dimensional array

The following code creates a two-dimensional array:

int myArray[][] = new int[4][6];

This code creates a new two-dimensional array with the dimensions as 4 and 6. So basically, myArray is an array having 4 elements. Each element in the array is an array of 6 elements.

So it will store data as follows:
{(2,4,6,8,10,12),  // 4 arrays, each having 6 elements
(3,6,9,12,15,18),
(4,8,12,16,20,24),
(5,10,15,20,25,30)}

(b) Accessing elements in a Two-dimensional array

In order to access an element in a multi-dimensional array, you need to specify the position for each dimension as follows:

int val = myArray[2][4]; 
// a will be assigned the element at row index 2 and column index 4

Just like one dimensional arrays, array index begins at 0 even for multi-dimensional arrays. So if we consider the data above, the value 20 will be assigned to the variable val

3. Creating and Using a 3-Dimensional Array

Similarly, you can create a 3-dimensional array as follows:

int myArray[][][] = new int [2][3][4];
myArray[1][0][3] = 5;

This code declares a 3-dimensional array with 2,3,4 as the dimensions. It sets the element at position 1,0,3 to 5.

Conclusion

So this article covers what arrays are and how you can use them. It also walks you through multi-dimensional arrays.

Exit mobile version