Subscribe
Tutorial search

Java arrays - Java tutorial


This tutorial explains the one dimensional and two dimensional arrays in Java
Category: Java tutorials - Difficulty:




Introduction

Array, what is it? An array is a group of variables of the same data type and referred to by a common name. An array is contiguous block of memory locations referred by a common name.

For e.g.

To store the marks of 5000 students, you can declare an array, marks, of size 5000 and can store the marks of as many students.

int marks[] = new int[5000];

Arrays

Why array is needed?

You might come across a situation where you need to store similar type of values for a large number of data items.

For e.g.

To store the marks of all the students of a university, you need to declare thousands of variables. In addition, each variable name needs to be unique. To avoid such situations, you can use arrays.

An array consists of a name and the number of elements of the array. You can refer to a specific array element by the array name and the element number, which is known as the index number.

Note: - Array index element number always starts with 0(zero).

Creating Arrays

The length of an array is fixed at the time of its creation. An array represents related entities having the same data type in contiguous or adjacent memory locations. The related data having data items form a group and are referred to by the same name.

For e.g.

employee[5];

Here, the employee is the name of the array and of size 5. The complete set of values is known as an array and the individual entities are called as elements of the array.

A specific value in an array is accessed by placing the index value of the desired element in a square bracket.

Advantages of using Arrays

  1. You can refer to a large number of elements by just specifying the index number and the array name.
  2. Arrays make it easy to do calculations in a loop.

The various types of arrays in java are:

  • One-dimensional arrays
  • two-dimensional arrays

One-dimensional Arrays

One-dimensional array is a list of variables of the same data type.

Syntax to declare a one-dimensional array

type array_name []; //type is the datatype of the array.

For e.g.

String designations []; // designations is name of the array.

Allocating Memory to Arrays

The new operator is used to allocate memory to an array.

Syntax to allocate memory

array_name = new type[size];

For e.g.

designations = new String[10]; //size of the array is 10.

Two-dimensional Arrays

In additions to one-dimensional arrays, you can create two-dimensional arrays. To declare two-dimensional arrays, you need to specify multiple square brackets after the array name.

Syntax to declare a two dimensional array

type array_name = new type[rows][cols];

For e.g.

int multidim[] = new int[3][];

In a two-dimensional array,

  1. You need to allocate memory for only the first dimension.
  2. You can allocate the remaining dimensions separately.
  3. When you allocate memory to the second dimension, you can also allocate different number to each dimension.

For e.g.

int multidim[] = new int[3][];

multidim[0] = new int[1];

multidim[1] = new int[4];

Accessing Arrays

You need to access various elements of an array to assign, retrieve, and manipulate the values stored in the array.

Assigning values to the Elements of an Array

To access a specific array,

  1. You need to specify the name of the array and the index number of the element.
  2. The index position of the first element in the array is 0.

For e.g.

String designations[];

designations = new String[2];

designations[0] = “General Manager”;

designations[1]=”Managing Director”;

You can declare and allocate memory to a user-defined array in a single statement.

Syntax

type arr [] = new type[size];

For e.g.

int employees[] = new int[10];

You can also declare and initialize arrays in the same statement.

For e.g.

String designations[] = {“General Manager”, ”Managing Director”};

Accessing values from various Elements of an Array

You can access values from elements in the array by referring to the element by its index number.

For e.g.

String designations[];

designations = new String[3];

designations[1] = “General Manager”;

designations[2]=”Managing Director”;

designations[0]=designations[2];

In the above example, the value of the third element of the array is assigned to the first element of the array.

Simple Java Application using Arrays

import java.io.*;

class student

{

int regno,total;

int mark[];

String name;

public student(int r,String n,int m[])

{

regno=r;

name=n;

mark=new int[3]; //new operator is used to allocate memory to an array.

for(int i=0;i<3;i++)

{

mark[i]=m[i];

if(mark[i]>50)

total+=mark[i];

else

{

total=0;

break;

}

}

}

public void displaystudent()

{

System.out.println("NAME:"+name);

System.out.println("REGNO:"+regno);

System.out.println("TOTAL:"+total);

}

}

class secondsample

{

public static void main(String args[])

{

int mk1[]={73,85,95}; // declare and initialize arrays in the same statement.

int mk2[]={71,85,95};

student st[]=new student[2];

st[0]=new student(1,"Ganguly",mk1);

st[1]=new student(2,"Sachin",mk2);

for(int i=0;i<2;i++)

st[i].displaystudent();

}

}

Output

javac secondsample.java

java secondsample

NAME:Ganguly

REGNO:1

TOTAL:253

NAME:Sachin

REGNO:2

TOTAL:251

In the above sample application different ways of array declaration and initialization has been illustrated.