What is Array in java with example

We have covered all the core concepts related to an array. What are they? How array can be initialized? what is the process of memory representation ? with a detail explanation of examples.

What is an Array?

Suppose if you want to store a single value you can easily use int and other data types as well but what if you want to store the value in a bunch? an array is a solution to such problems.

  • To deal with a collection of similar data items, java offers a reference data type called arrays.
  • An array can hold different values of the same type.
  • An array consists of a contiguous memory location. The lowest address corresponds to the first element and the highest address to the last element. An array is a way to group a number of items into a larger unit.
  • Array can have data items of simple4 types like int or float, or even of user-defined types like structure and objects.

Types of arrays:

There are basically two different types of arrays.

  1. Single Dimensional Array
  2. Multi-Dimensional Array

What is Single dimensional array in Java?

This is the simplest form of an array. The array given name and its elements are referred to y their subscripts or indices. Java array’s index numbering starts with 0(zero).

The general form of an array declaration is shown below:

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

or

type[] arrayname = new type [size];

Where the types declare the base type of the array, which is the type of each element in the array. The array-name specifies the name with which the array will be specified, here size defines how many elements the array will hold. the size must be an integer value or integer constant without any sign.

for instance :

we can have,

double x[];

x= new double[5];

These steps can be combined on a single line, as shown below:

double x[]= new double[5];

There is one thing note about the array that they can also be initialized at the time of declaration.

Here is an example:

Program to read marks of 5 students and store them under an array.

import java.io.DataInputStream ;
class ar1
{
public static void main(String args[])
{
DataInputStream in= new DataInputStream(System.in);
final int size=5;
int i;
int marks[]=new int[size];
for(i=0;i<size;i++)
{
System.out.println("Enter marks of student" +(i+1)+" :");
marks[i] =Integer.parseInt(in.readLine());
}
}
for(i=0;i<size;i++)
System.out.println("Marks["+(i+1)+"] =" +marks[i]);
}
}

Output:

Enter marks of Student1 :67
Enter marks of Student2 :78
Enter marks of Student3 :90
Enter marks of Student4 :79
Enter marks of Student5 :70
Marks[1] = 67
Marks[2] = 78
Marks[3] = 90
Marks[4] = 79
Marks[5] = 70

Memory representation:

Internally, the array is stored as a special object containing:

  • A group of contiguous memory locations that all have the same name and same datatype.
  • A reference that stores the beginning address of the array elements.
  • A sperate instance variable containing the number of elements in the array.

What is a Multi-dimensional array?

A two-dimensional array is the simplest of multidimensional arrays. Java allows arrays of more than two dimensions.

The general form of a two-dimensional array declaration in java is as follows:

type array-name[][]=new type[row][columns];

or

type[][]array-name= new type[rows][columns];

Where type is the base data type of the array having name array-name; rows, the first index, refers to the number of rows in the array and columns, the second index, refers to the number of columns in the array.

for example the declaration an int array of size 5,12.

int sales[][]=new int [5][12];

or

int[][]sales= new int [5][12];

Here is an example:

Program to read sales of 5 salesmen in 12 months and to print total sales made by each salesman.

import java.io.DataInputStream ;
class Saleman
{
public static void main(String args[])
{
DataInputStream in= new DataInputStream(System.in);
int sales[][]=new int[5][12];
int i,j,total;
for(i=0;i<5;i++)
{
total=0;
System.out.println("Enter sales of salesman"+(i+1));
for(j=0;j<12;j++)
{
System.out.println("total sales of saleman"+(i+1)+" ="+total);
}
}
}
}

Sample output of the following:

Enter sales of salesman 1
Month 1 : 5000
Month 2 : 6000
Month 3 : 7000
Month 4 : 4000
Total sales of salesman 1 = 22000
Enter sales of salesman 2
Month 1 : 5000
Month 2 : 4000
Month 3 : 4400
Month 4 : 6000
Total sales of salesman 2 = 19400