Elshad Karimov

An array is a very powerful data structure that stores lists of elements, including objects. With endless applications, arrays are one of the most used data structures in Java. To better understand what an array is, we can look at simple examples of real-life arrays, like a collection of books on your table, clothes in a wardrobe, and pens in your pen stand.

Array structures in real life

This box of macarons is very similar to array data structures in Java. If we look at this box very carefully, we can easily see point out its main properties.

Java Data Structures and Algorithms Masterclass

Last Updated February 2022

  • 426 lectures
  • All Levels
4.5 (1,635)

Data Structures and Algorithms from Zero to Hero and Crack Top Companies 100+ Interview questions (Java Coding) | By Elshad Karimov

Explore Course

Compare the properties of this box of macarons with the array structure below.

six numbers placed side by side like the macarons in the box

Here, we have six elements placed side by side like the macarons in the box. Let’s examine the properties of an array to find more similarities. 

Why are arrays useful?

In computer science, an array is a data structure consisting of elements, each identified by at least one array index or key. An array is stored so that the position of each element can be computed from its index by a mathematical formula.

But why do we need an array? 

In software programs, the array is used almost everywhere in the program to store any data. Consider a situation where we need to store three integer numbers. If we use programming’s simple variable and data type concepts, then we need three variables of int data type, and the program will be as follows:

int main() {
  int num1;
  int num2;
  int num3;
}

It is very simple because we had to store just three integer numbers. Now let’s assume we have to store 500 integer numbers. Are we going to use 500 variables?

To handle such situations, almost all the programming languages provide a concept called an array. Instead of declaring individual variables, such as number1, number2, …, number99, you just declare one array variable number of integer type and use number[0], number[1], and …, number[99] to represent individual variables. Here, 0, 1, 2, …..99 are indexes associated with variables, and they are being used to represent individual elements available in the array.

On a higher level, it’s easier to perform transactions such as a binary search, sort algorithm, search algorithm, or search tree on linked lists like this. Java isn’t unique — arrays are important throughout computer science.

Types of array data structures and algorithms in Java

There are two types of arrays, depending on the number of dimensions. They can be one-dimensional or multidimensional. In multidimensional arrays, we can see that they can be two-dimensional, three-dimensional, and so on. 

One-dimensional arrays

The simplest type of array is the linear array, which is also known as the one-dimensional array. 

This array will have a bunch of values declared with a single index. 

A one-dimensional array has a single row and multiple columns in it, as shown here: 

An example of a one-dimensional array.

One-dimensional array elements can be accessed using a[i] where a is the name of the array and i is an index between 0 and n.  If we want to access the number 10 from this array, we would use 2 as the index. 

a[2] // a is the name of array
//10

Two-dimensional arrays

A one-dimensional array consists of one row and multiple columns, but in a two-dimensional array, the number of rows is more than one, representing a typical matrix. 

An example of a two-dimensional array.

It is an array with a bunch of values having been declared with a double index. Notice how the array above has multiple columns and rows. If we look closely, we can see that the two-dimensional array is composed of one-dimensional arrays. In this case, it seems that we combined three one-dimensional arrays and formed this two-dimensional array. 

To access elements of two-dimensional arrays, we have indexes for columns and rows. For example, to access 20 in the first row, we need to find the index of the row first, then the index of the column. In this case, it is:

a[0][4]
  //20

Similarly, if we want to find the address of this element here:

a[2][5]

So we first put the row number then the column number. 

Three Dimensional Array

The next type of array is a three-dimensional array. It is similar to a two-dimensional array, but here we have depth. We can imagine a three-dimensional array as multiple two-dimensional arrays, and values are declared with three indexes — because as we said, there is depth. The three-dimensional array can be represented by a cube with numbers on it: 

Three dimensional cube with numbers on each individual square.

If we look at it carefully, we can see that each face of this cube is composed of two-dimensional arrays, which means that a three-dimensional array is a combination of two-dimensional arrays.

In the real world, one-dimensional and two-dimensional arrays are used very broadly, but three-dimensional arrays are rarely used. 

To access the elements of the array, we first need to find the depth of the array. Then we need to find the row number, and eventually the index of the column. So if we want to access the number 2 from the red side, we will write something like this:

a[0][0][1]
//2

Let’s try one more example. If we want to access an element of 3 on the white side of this cube. The depth is 2, and the row index is 0, and the column index is 1. We can access it like this:

A[2][0][2]
//3

Arrays in memory

Let’s say we have an array like this:

array example

To see how it is located in the memory, let’s assume our random access memory (RAM) is like this grid shown below:

array located in grid

When we initialize an array of nine elements, the compiler allocates nine contiguous cells in the memory, like this. Based on the system load, the compiler decides where the initial cell starts on RAM. We as developers cannot do anything about this, because it is up to the system from which location this array will start.

The system guarantees that all these cells will be contiguous in the memory. It is not possible that cell 1 will be apart from cell 2 in the memory. All of them must be next to each other because this is the nature of an array. 

So the representation of a one-dimensional array in memory is very simple. Basically, the compiler allocates from any given cell and then allocates 10 contiguous cells.

Now let’s see how a two-dimensional array is represented in the memory. Let’s say we have an array of 3 columns and 3 rows: 

example of array of 3 columns and 3 rows

How is this going to be represented in the memory? In the memory, it is represented as a one-dimensional array. So row number 0 and column number 0 come first, then row number 0 and column number 1, then row number 0 and column number 2, and it continues like this.

array represented one-dimensional in memory

As you can see, a two-dimensional array is represented in memory row by row, by combining all the rows together in one row. It is not stored in the memory like the way it was shown previously. This was just to give users a clear picture of it. So, on a physical level, it is stored as a one-dimensional array. 

How to create an array in Java

To create an array in Java, we have three steps:

  1. Declare. Creates a reference to an array.
  2. Instantiation of an array. Creates an array.
  3. Initialization. Assigns values to cells in an array.

Let’s understand these three steps using this diagram. Let’s say this is our system RAM having multiple memory blocks:

grid representing RAM

Now let’s see what the compiler does when we create a new array. The first step is we declare an array, so when we declare an array, the system never allocates for that array. All it does is create a reference variable. Let’s say I have named my array arr, so the reference variable is the name of the array.

In the memory, all the system does is create a reference variable like this:

grid containing one reference variable

The next step is instantiation of the array. So when we instantiate an array, the compiler allocates memory for this array. So let’s say our array is the size of five, so the system allocates five contiguous cells for this array over here. 

Now, you might be interested in what the values will be for these cells. It’s set at 0 by default, so the compiler will store the address of the first cell in this reference variable.

Let’s say the address of this first cell is xx111. The reference variable will save the address of this first cell at this point. As soon as this reference variable saves this address of the first cell, the reference gets created from custom Array to the first cell of this array. 

Now let’s say we want to access the first element. If so, the system takes the address of the reference variable plus 0. Now you might understand why we start from the 0 index —  because the reference variable references the address of the first cell. If we start from the first index, we can’t access the first cell if we add one. By adding 0, we are accessing the address of the first cell. So it only needs to increase the counter from the second cell onward. 

example of instantiation of the array

After instantiation of an array, we continue to the last step, which is the initialization of an array.

In this step, we need to give a default value to the cells. Right now, the compiler gives a default value of 0. 

Let’s say we want to give a default value of 10 in each of these cells. We need to initialize them. After initialization, our array will look like this so all the cells have a value of 10:

example of initialization of an array

Now with these three steps, we can easily create an array in Java, and the syntax of creating an array in Java is like this. First, we write a data type with a square bracket and the name of the array. The data type can be different. It can be an integer, float, or string. 

In the next step, we instantiate an array, so the syntax for that one is the reference name, then the new keyword and data type with size in square brackets.

The last step is initializing an array, so we just put the array name and index inside the square bracket.

//Declaration  
  int[] intArray;
//Instantiation
  intArray = new int[3];
//Initialization
  intArray[0] = 1;
  intArray[1] = 2;
  intArray[2] = 3;

Additionally, there is a shortcut. By writing data type and name of array and values inside curly brackets, we can create an array like this in Java. With this, we can combine all these three steps into one.

// All together
  String sArray[] = {“a”, “b”, “c”};

Learn more about Java arrays

Learning data structures and algorithms in Java is important for any beginner programmer. Still, arrays are just one important element of working with Java. It’s one of the first things you’ll learn — but an array itself doesn’t do much. 

You’ll still need to learn more about Java as a programming language to start taking advantage of the data in the array and building actual Java programs. You can begin by looking at some existing Java code, or take on a bootcamp or lesson.

Page Last Updated: April 2022

Top courses in Java

Learn Selenium with Java, Cucumber + Live Project
Pavan Kumar
4.6 (1,087)
Highest Rated
Java Programming Masterclass covering Java 11 & Java 17
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.6 (161,470)
Bestseller
Java for Absolute Beginners
Nick H
4.6 (4,849)
Design Patterns in Java
Dmitri Nesteruk
4.3 (6,620)
Java Design Patterns
Bharath Thippireddy
4.6 (1,306)
Bestseller
Java Programming for Complete Beginners
in28Minutes Official
4.5 (24,692)
Modern Java - Learn Java 8 features by coding it
Pragmatic Code School
4.5 (5,210)
Learn Advanced Java
John Purcell
4.6 (193)
The Complete Java Development Bootcamp
Learn The Part Inc., Rayan Slim, Jad Slim, Jose Portilla
4.7 (1,806)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo