Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Java Array

Java Array

May 2nd, 2019

Java Array

Arrays are sequential memory locations can be used to hold a list of numerous data of same data type, so that we can access them (supply them) as a bunch for a process.
When we have numerous data belonging to same data type, we can’t keep on assigning them & process them one by one in our program manually, particularly when all those list of data are going to participate in the same operation one after the other. So, this kind of hard task has become easy by using arrays.
In java, these arrays are dealt in the form of an object for data protection. It is just like storing numerous values in numerous variables & using it in a process. The contents(data) are stored in indexes and the indexes starts from 0. For example, if the total number of data is 10. These data are allotted starting from 0th index till 9th index (10-1). [i.e, if we start storing contents from index1 then it could be till index10. But, since it is not possible as by default index starts from 0, we should start storing data from 0th index, and hence the last index becomes index9 for storing 10 elements.]

Types of Array :

  1. Single Dimensional Array.
  2. Two Dimensional Array.
  3. Multi Dimensional Array.

Single Dimensional Array :

Single Dimensional array are just like a simple list of data. The data is stored simply one after the other in consecutive indexes starting from 0 till (the total number of data-1).

Syntax:

DataType[] arrayName=new arrayName [size];
or
DataType arrayName[]=new arrayName [size];
or
DataType[] arrayName; or DataType arrayName[];
arrayName=new DataType[size];
Now here,
‘DataType’ refers to the type of collection of data the array can hold.
‘[ ]’ is the representation of an array. Single ‘[ ]’(square bracket) defines single dimensional array.
‘arrayName’ could be any name representing just the name of an array.
‘new arrayName[size]’ represents creation of an array object which is going to hold an array(set of memory locations) of given ‘size’. It is must to mention the size of an array while creating an array object within the square bracket. This object is assigned to the reference ‘arrayName’.
Here, we are going to declare an array of size 4, which means it can hold 4 contents, 0th index to 3rd index (Not more, not less).
i) int a[]=new int[4];
or
ii)  int[] a=new int[4];
or
iii) int[] a; or  int a[];
a = new int[4];

Storing values inside Single Dimensional Array : 

a[0]=10;
a[1]=20;
a[2]=15;
a[3]=25;

For retrieving values stored in an array, either we use for loop or for each loop.

for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}

Sample Program :

public class Array
{
public static void main(String[] args)
{
int n=4;
int[] a=new int[4];
a[0]=10;
a[1]=20;
a[2]=15;
a[3]=25;
System.out.println("Result : ");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}

Output :

10
20
15
25
length :- It is a property to find the length of the given array. eg. System.out.println(a.length); // shows the size of the array.

Two Dimensional Array :

Two Dimensional array is just like one array inside another array. The total number of elements to be stored inside 2-D array will be the multiplication of the two sizes. For displaying the elements of array you have to use two parameters.

Syntax :

DataType[][] arrayName=new arrayName [size1][size2];
or
DataType arrayName[][]=new arrayName [size1][size2];
or
DataType[] arrayName[]=new DataType[size1][size2];
Now here,
‘DataType’ refers to the type of collection of data the array can hold.
‘[ ]’ is the representation of an array. Two ‘[ ]’(square bracket) defines Two dimensional array.
‘arrayName’ could be any name representing just the name of an array.
‘new arrayName[size1][size2]’ represents creation of an array object which is going to hold an array(set of memory locations) of given ‘size1*size2’. It is must to mention the size of an array while creating an array object within the square bracket. This object is assigned to the reference ‘arrayName’.
Here, we are going to declare an array of size(3, 4), which means it can hold 3*4=12 contents, [ 0,0]th index to [2,3] index (Not more, not less).
i) int a[][]=new int[3][4];
or
ii) int[][] a=new int[3][4];
or
iii) int[] a[] = new int[3][4];

Storing values inside Two Dimensional Array : 

a[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[0][3]=4;
a[1][0]=5;
a[1][1]=6;
a[1][2]=7;
a[1][3]=8;
a[2][0]=9;
a[2][1]=10;
a[2][2]=11;
a[2][3]=12;
 

For retrieving values stored in an array, either we use for loop or for each loop.

for(int i=0;i<3;i++)
{
for(int k=0;k<4;k++)
{
System.out.println(a[i][k]);
}
}

Sample Program :

public class Array {
public static void main(String[] args) throws Exception
{
int[][] a=new int[3][4];
a[0][0]=1;
a[0][1]=2;
a[0][2]=3;
a[0][3]=4;
a[1][0]=5;
a[1][1]=6;
a[1][2]=7;
a[1][3]=8;
a[2][0]=9;
a[2][1]=10;
a[2][2]=11;
a[2][3]=12;
System.out.println("Result : ");
for(int i=0;i<3;i++)
{
for(int k=0;k<4;k++)
{
System.out.println(a[i][k]);
}
}
}
}

Output :

1
2
3
4
5
6
7
8
9
10
11
12

Multi- Dimensional Array :

Just like 2-D array, we have multi-dimensional array. Whatever the number of dimensions we use, the only thing we need to remember is we must provide the size of at least first dimension or first two or first three dimensions like that. It should be contiguous. We can leave empty square brackets later but not in the middle.
int[][] a[][]=new int[2][3][2][3];
or
int a[][][][]=new int[2][3][2][];

  • For the empty square brackets the more number of data you give, that much size will be taken.
  • In this example, since it is 4-D, while retrieving we need to use 4 parameters or variables.

Dynamic Array :

Dynamic array is the array in which you don’t specify the size of the array but you need to specify the values initially while declaring the array. Dynamic array can be applied to any dimensions of array. For the dimensions specification, you need to differentiate it when you give data using curly brackets.
Now, we are going to see the same program which we saw previously by using Dynamic Array.

Example 1 :

public class Array {
public static void main(String[] args) throws Exception
{
int[] a[]= {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; //Dynamic Array Declaration
System.out.println("Result : ");
for(int i=0;i<3;i++)
{
for(int k=0;k<4;k++)
{
System.out.println(a[i][k]);
}
}
}
}

Output :

1
2
3
4
5
6
7
8
9
10
11
12
Here, the size of the given dynamic array is [3][4] because here we have 3 sets of data and within each set there are 4 elements inside.

Example 2 :

public class Array {
public static void main(String[] args) throws Exception
{
int a[][]= {{1,2},{4,5}};         // Dynamic Array Declaration
System.out.println("Result : ");
for(int i=0;i<2;i++)
{
for(int k=0;k<2;k++)
{
System.out.println(a[i][k]);
}
}
}}

Output :

1
2
4
5
Here, the dimensions for the given dynamic array is [2][2], since it has 2 sets of data within which each set has 2 elements.