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.

• It is a box designed to hold macarons in it, and this means that this box is produced for macarons. You cannot store cream puffs in it.
• All macarons in this box are next to each other. There is no gap between them, meaning they are contiguous.
• Each macaron here can be identified uniquely based on its location. If someone asked if they could try the third macaroon, you would know that they were referring to the pink macaron placed third in the box.
• We cannot change the size of the box, which is fixed. This means that this box is produced by some standard manufacturer and assumes that there will be only 10 macarons in it. So we cannot change the size of this box.
Java Data Structures and Algorithms Masterclass

Last Updated November 2022

• 425 lectures
• All Levels
4.4 (3,294)

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

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

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.

• An array can only store data of a specified type. Once we define the type of data for an array, the elements must be the same type of data that we defined. This means that when we create an array of integers, we cannot store string values in the array. If we tried to do so, it would raise an error. Similarly, we wouldn’t be able to store a a cream puff inside the box of macarons. Macarons are the defined data type of the box, or the array, so any other types of sweets wll not fit.
• The elements of an array are located in a contiguous location in memory. In other words, the elements are located next to each other. There is no gap between them, as we noted with the macarons.
• Each element of an array has a unique index. The index of the fourth element is 3, meaning that each element has a unique index starting from 0. So the first element starts from 0 and increases sequentially.
• The last property is the size of an array, which is predefined and cannot be modified. If we take another look at this box of macarons, we see that the size of the box is predefined. This is applicable for an array. When we create an array, we also define its capacity, which is one of the biggest limitations of an array.

## 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:

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:

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.

### 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.

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:

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

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:

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:

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:

## Arrays in memory

Let’s say we have an array like this:

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

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:

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.

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:

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:

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.

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:

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.

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.

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

Java Programming Masterclass updated to Java 17
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.5 (176,595)
Bestseller
Java Programming for Complete Beginners
in28Minutes Official
4.6 (31,903)
Modern Java - Learn Java 8 features by coding it
Pragmatic Code School
4.5 (7,769)
Java Collections from basics to Advanced
Basics Strong
4.4 (986)
Bestseller
Complete Core Java In Simple Way
DURGASOFT NAGOOR BABU
4.5 (640)
The Complete Java Development Bootcamp
Learn The Part Inc., Rayan Slim, Jad Slim, Jose Portilla
4.6 (5,602)
Develop Minecraft Plugins (Java)
Stephen King
4.8 (3,220)
Bestseller