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.

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

JAVA TUTORIALS

Recent Posts

RELATED POSTS

Remove Duplicates from List Using For-Loop

The simplest way to remove duplicates from a 'List' is to use a for-loop. The following code snippet demonstrates this method. First, the code creates...

Java DecimalFormat Class

When you need to format decimal numbers, such as taking three or two decimal places for a number, showing only the integer part of...

Convert List to Array Using For-Loop

The simplest way to convert a List to an array is to use a for-loop. The following code snippet demonstrates this method. First, the code...

How To Convert List To Array in Java

A common scenario faced by programmers is to convert a Java List to an Array. A 'List' is part of the Collection Framework and...

Â

CHECKOUT 'HOW-TOs'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string...

MORE ON CODEX

MORE IN THIS CATEGORY

COM Components and Scripting Process

Although this article should not concern you at all as a 'script developer' as long as you are aware of creating a reference to...

Convert List to Array Using Stream with Param

There is an overloaded version of the Stream.toArray method which can be used to return a result array that is of the same data...

Convert String to Date Using java.util.Calendar

The java.util.Calendar class also encapsulates a date. The Calendar class has some more features than the java.util.Date class. It provides the ability to extract...

Format Decimal Numbers Using Format Symbols

You can customize which symbols are used as decimal separator, grouping separator, currency seperator etc. using a DecimalFormatSymbols instance together with java.text.DecimalFormat class. The...

OTHER TUTORIALS

Remove Duplicates from List Using Set.addAll

The Set interface has a method called addAll. This accepts a 'Collection' as the parameter. So if you invoke...
- Advertisement -spot_img