Array in Java


Java Array

In Java Programming,it becomes necessary to store data within the memory in the most convenient and ecnomical way.We may need to structure the memory to stor numerous data items by applying minimum set of variables and by using optimum memory space.This will done with the help of dimensional array.

 

 

Array Definition:

A dimensional array is a structure created in the memory to represent a number of teh same type of data (i.e integer/string/characters)withg the help of a single variable.

Creating ,Initializing,and Declaring Array Variables:

One way to create an array is with the new operator. To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable:

e.g  int a[]=new int[10];

Advantage of Java Array

  • Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
  • Random access: We can get any data located at any index position.

Disadvantage of Java Array

  • Size Limit: We can store only fixed size of elements in the array. It doesn’t grow its size at runtime. To solve this problem, collection framework is used in java.

Types of an Array

 

A dimensional array is two types.

1. Single dimensional Array

2. Multiple Dimensional Array

arr2

 

 

 

 

 

 

 

 

 

 

 

1. Single dimensional Array

When the elements are specified by a single Subscript,the array is called Single dimensional array or Single Sub-scripted variable.

e.g int a[]=new int[10];

This mean it can store 10 data items in the memory with different subscripts,However ,the storage of the array elements begin with 0th position as below:

arr1

 

Where 0,1,2……………………..,9 are referred as subscripts and they help in both input and output of the array elements.a is subscripted variable which is used to represent data items at different subscripts as a[0],a[1]…….,a[9].

The above example explain how an array can store  10 different numbers of the same type in the continuous location of the memory starting with 0th position.

 

We can create arrays to store data of different types as shown below:

arr3

 

 

 

Using array Elements :

Syntax :<array name> <subscript>

e.g : a[2] means to operate on the data stored at the 2nd subscript.

However,the data can be stored in array based programs in two different ways.

(1) By using assignment statement

(2) By using Input Stream

 

(1) By using Assignment Statement:

In this type, the data values are defined within the program itself.it may be an integer,a string,a char or a float types.

 

(a) For Integer type:

The value are stored in an array as given below:

e.g : int a[]={11,23,12,67,45}

 

A program is illustrate below:

public class abc

{

public static void main(String args[])

{

int i,sum;

sum=0;

int a[]={3,4,5,6,7};  //initializing an array

for(i=0;i<5;i++)

{

sum=sum+a[i];

System.out.println(“The sum of the array elements is=”+sum);

}

}

output:

The sum of an array elements is = 22

In above program ,the array elements are accessed through a[i] simultaneously by changing i from 0 – 4(zero to four)

 

(b) For character type:

The characters  are stored in an array as given below:

e.g : char  ch[]={‘J’,’A’,’V’,’A’}

A program is illustrate below:

public class abc

{

public static void main(String args[])

{

char ch;

ch[]={‘J’,’A’,’V’,’A’}  //initializing an array

System.out.println(The character of an array elements are:”);

for(i=0;i<4;i++)

{

System.out.println(ch[i]);

}

}

output:

JAVA

 

(b) For String type:

The String  are stored in an array as given below:

e.g : String  ch[]={“Java”,”programming”,”language”}

A program is illustrate below:

public class abc

{

public static void main(String args[])

{

String ch[]={“Java”,”programming”,”language”}//initializing an array

System.out.println(The lanuage name is:”);

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

{

System.out.println(ch[i]);

}

}

output:

The language name is:Java programming lanuage

 

(2). By using Input Stream:

In this type also, the data values are to be entered at the time of executing the program.It may be an integer, a String, a character or a fractional  type.

 

(a) For Integer type : 

The data values are to be entered at the time of execution one by one from the console.

However,it is necessary to give a relevant message for using Input statement to enable Input editor.

Syntax : int a[]=new int[10];

which means it creates locations in the memory to store 10 different numbers of same type.

A complete program is illustrated below to make use of Input Statement in arrays:

import java.io.*;

Public class Simple {

public static void main(String args[])

{

InputStreamReader read=new InputStreamReader(System.in)

BufferedReader in=new BufferedReader(read);

int a[]=new int a[5];

int i,sum;

sum=0;

for(i=0;i<5;i++)

{

System.out.println(“Enter your marks in the cell”);

a[i]=Integer.parseInt(in.readLine());

}

for(i=0;i<5;i++)

sum=sum+a[i];

System.out.println(“the sum of the array elements =”sum);

}}

(b) For character type:

All the characters are to be entered at the time of execution from the console to get the desired oputput.

However,it is necessary to give a relevant message for using Input statement to enable  Input editor.

Syntax : char ch[]=new char[10];

A complete program is illustrated below to make use of Input Statement in arrays:

import java.io.*;

Public class Simple {

public static void main(String args[])

{

InputStreamReader read=new InputStreamReader(System.in)

BufferedReader in=new BufferedReader(read);

char ch[]=new char ch[10];

int i,j;

for(i=0;i<5;i++)

{

System.out.println(“Enter all the character at a time:”);

ch[i]=(char)(in.read());

}

for(j=0;j<5;j++)

System.out.println(ch[j]);

}}

(b) For String type:

The data is to be entered at the time of execution one by one from the console.

However,it is necessary to give a relevant message for using Input statement to enable  Input editor.

Syntax : String ch[]=new String[10];

A complete program is illustrated below to make use of Input Statement in arrays:

import java.io.*;

Public class Simple {

public static void main(String args[])

{

InputStreamReader read=new InputStreamReader(System.in)

BufferedReader in=new BufferedReader(read);

String ch[]=new String ch[10];

int i;

for(i=0;i<5;i++)

{

System.out.println(“Enter the name in cell:”);

ch[i]=in.readLine();

}

for(i=0;i<5;i++)

System.out.println(ch[i]);

}}

 (2). Multidimensional Array:

More than one dimension array is known as multidimensional array.Multidimensional array is also known as Two dimensional array or three dimensional array.In this we use two subscript for array.one for row and second for column.

Syntax : int a[][]=new int a[2][2];

This mean that it create two row and two column in an array.like below.

2  3

4  5

Example to initialize Multidimensional Array in java

  1. a[0][0]=1;
  2. a[0][1]=2;
  3. a[0][2]=3;
  4. a[1][0]=4;
  5. a[1][1]=5;
  6. a[1][2]=6;
  7. a[2][0]=7;
  8. a[2][1]=8;
  9. a[2][2]=9;

Example of Multidimensional java array

Let’s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.

class simple{

public static void main(String args[]){

//declaring and initializing 2D array  

int a[][]={{1,2,3},{2,4,5},{4,4,5}};

//printing 2D array  

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

for(int j=0;j<3;j++){

System.out.print(a[i][j]+” “);

}

System.out.println();

}  }}

Output: 

1 2 3

2 4 5

4 4 5

Addition of 2 matrices in java

Let’s see a simple example that adds two matrices.

class Testarray5{

public static void main(String args[]){

//creating two matrices  

int a[][]={{1,3,4},{3,4,5}};

int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices  

int c[][]=new int[2][3];

//adding and printing addition of 2 matrices  

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

{

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

{

c[i][j]=a[i][j]+b[i][j];

System.out.print(c[i][j]+” “);

}

System.out.println();//new line 

}   }}

Output:

2 6 8

6 8 10

Comments are closed.