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

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 #7 Part 1 – Classes & Objects

Classes are a very important concept in Java. Java code always needs to be written in a class. In this article, we will be...

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 this method by passing the...

Concatenate Strings Using String Joiner

Java 8 has added a new class called StringJoiner. This concatenates Strings using a delimiter as well as adds a prefix and suffix to...

Â

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

Format Decimal Numbers Using Locale

If you want to create a DecimalFormat instance for a specific Locale, create a NumberFormat and cast it to a DecimalFormat. The java.text.DecimalFormat class is...

Interpreted Vs Compiled Languages

This is based on an excerpt from one of my favourite literature on VBScript and in fact, is quite relevant with respect to one...

Convert String to java.time.LocalDate with Formatter

The LocalDate class has an overloaded parse() method. In addition to the String Date, it accepts a DateTimeFormatter instance that specifies the format of...

Getting Started With Hibernate

Introduction Hibernate is a framework which allows you to access database tables from Java code. Before Hibernate came into existence, JDBC was widely used. JDBC...

OTHER TUTORIALS

How To Install WordPress Locally using XAMPP

1. Introduction Installing WordPress on your computer helps you try out WordPress, test themes and plugins, and learn WordPress development. ...
- Advertisement -spot_img