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 Stream

Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() method has been...

Concatenate Strings Using String.concat()

The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following...

Java Tutorial #2 – Operators in Java

Table of Contents Introduction 1. Assignment Operator 2. Arithmetic Operators 3. Compound Operators 4. Increment & Decrement Operators 5. Relational Operators 6. Logical Operators Introduction Operators are an essential...

Convert List to Array Using ToArray Without Param

Java provides a toArray method on the 'List' interface. This can be used to convert a 'List' to an array. Since there are are...

Â

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

VBS Part 1 – Introduction and Background

At the upfront, this theoretical article might look boring to most of us but still, it is advisable to know about our scripting friend...

How To Create Maven Project in Eclipse With Archetype

Maven Basics Maven automates the steps involved in building a software application like adding the JAR files, compiling code, running unit tests, creating the output...

Finding Web Elements with Selenium

I'm going to explain in this tutorial about the usage of the findElement and findElements method of Selenium Webdriver on the Chrome web browser....

How To Create Maven Project Using Command Line

Maven project can be easily created using built-in plugins from the popular IDEs such as Eclipse and IntelliJ IDEA. However, in scenarios where you...

OTHER TUTORIALS

Working with JMeter Listeners

About Listeners Listeners are used for displaying test results in JMeter. Listeners allow system engineers to analyze the responses from...
- Advertisement -spot_img