Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

November 10, 2014 Categories: Core Java Basics. No Comments on Java Arrays

In this article you will learn about arrays, one of the frequently used data structure in programs. We will look at how to declare arrays, access array elements and working with one-dimensional and two-dimensional arrays.

An array is a set of homogeneous variables sharing the same name. Homogeneous in the sense, all the elements in the array must be of the same data type. All the elements of an array are stored side-by-side in the memory. Arrays are generally used when there is a need to manipulate a set of logically related data elements. Individual elements of an array are referred using an index value or also known as subscript. The index value of an array always starts from zero.

 

One-Dimensional Arrays 

An array can be one-dimensional, two-dimensional or multi-dimensional. Creation of a one-dimensional array is a two step process. First, we have to create an array variable and second; we have to use the new operator to allocate memory for the array elements.

Syntax for declaring an array variable is as shown below:

data-type array-name[ ];

Example for declaring an array is shown below:

int a[ ];

In the above example, int is the data type and a is the array name. One pair of square brackets ([ ]) indicates that the array is one-dimensional.

Syntax for allocating memory for the array elements is as shown below:

array-name = new data-type[size];

Example for allocating memory for the array elements is shown below:

a = new int[10];

In the above example, new is a keyword which allocates memory for 10 elements of the type int. In Java, all the arrays are allocated memory dynamically at runtime.

Two steps for creating an array can be combined into a single step as shown below:

int a[ ] = new int[10];

Array Initialization: 

Assigning values into array locations is known as array initialization. Array initialization can be static or dynamic. Static initialization involves specifying values at the time of declaring an array as shown below:

int a[ ] = {1,2,3,4,5,6,7,8,9,10};

When the above line is executed, an array of size 10 is created by JVM as shown in the below figure:

One Dimensional Arrays

Note that, position of 1 in the array is 1, whereas index or subscript of element 1 is 0. In general, the index or subscript of Nth element is N-1.

Dynamic initialization involves assigning values into an array at runtime. Following code segment demonstrates dynamic initialization of arrays:

Note: By default, when an array is created, all the elements of numeric type are initialized to zero, all elements of boolean type are initialized to false, all elements of type char are initialized to ‘\u0000’ and all reference types are initialized to null.

Accessing Array Elements:

Individual elements of an array can be accessed using the index or subscript. In Java, the subscript always begins at zero. So, the first element of an array a can be accessed as a[0] and second element with a[1] and so on Nth element can be accessed with a[N-1].

 

Two-Dimensional Arrays 

A two-dimensional array can be indicated by specifying two pairs of square brackets ([ ]) while declaring an array. A two-dimensional array can be created as shown below:

int a[ ][ ] = new int[3][3];

Above line creates an array with 3 rows and 3 columns (matrix) with a size of 3*3 = 9. So, the above array a is capable of storing 9 integer elements.

We can also declare a two-dimensional array by specifying only the number of rows as shown below:

int a[ ][ ] = new int[3][ ];

We can specify the number of columns for each row as shown below:

a[0] = new int[3];
a[1] = new int[3];
a[2] = new int[3];

So, by looking at the above example, we can say that a multi-dimensional array (2-D, 3-D, …, N-D) is an array of arrays.

The above way of declaration can be used to create uneven arrays which contain unequal number of columns for each row. Such kind of arrays is also known as jagged arrays.

Individual elements of a two-dimensional array can be accessed by specifying both row-index and column-index as shown in the below figure:

2D Arrays

 

Alternative Array Declaration Syntax 

Java supports another way to declare arrays as shown below:

data-type[ ] array-name = new data-type[size];

Example for declaring arrays using the above syntax is shown below:

int[ ] a = new int[10];

You might think what is the need for this alternative way of declaring an array? For example, you might want to declare three one-dimensional arrays. You can do it using the conventional syntax like:

int a[ ], b[ ], c[ ];

Instead, you can use the alternative syntax like:

int[ ] a, b, c;

which is more simple and more readable than the previous one.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me