Object Oriented Programming: Unit I: Introduction to OOP and Java

Arrays

with Example Java Programs

• Definition: Array is a collection of similar type of elements.• Using arrays the elements that are having the same data type can be grouped together.

Arrays

• Definition: Array is a collection of similar type of elements.

• Using arrays the elements that are having the same data type can be grouped together.

• If we use bunch of variables instead of arrays, then we have to use large number of variables.

• Declaring and handling large number of variables is very inconvenient for the developers. There are two types of arrays -

o One dimensional arrays

o Two dimensional arrays

One Dimensional Array

• Array is a collection of similar type of elements. Thus grouping of similar kind of elements is possible using arrays.

• Typically arrays are written along with the size of them.

• The syntax of declaring array is -

data_type array_name[ ];

and to allocate the memory -

array_name=new data_type[size];

where array_name represent name of the array, new is a keyword used to allocate the memory for arrays, data_type specifies the data type of array elements and size represent the size of an array. For example:

a= new int[10];

[Note that in C/C++ this declaration is as good as int a[10] ]

• After this declaration the array a will be created as follows

• That means after the above mentioned declaration of array, all the elements of array will get initialized to zero. Note that, always, while declaring the array in Java, we make use of the keyword new, and thus we actually make use of dynamic memory allocation.

. • Therefore arrays are allocated dynamically in Java. Let us discuss on simple program based on arrays.

Java Program [SampleArray.Java]

/*

This is a Java program which makes use of arrays

*/

class SampleArray

{

public static void main(String args[])

{

int a[];

a=new int[10];

System.out.println("\tStoring the numbers in array");

a[0]=1;

a[1]=2;

a[2]=3;

a[3]=4;

a[4]=5;

a[5]=6;

a[6]=7;

a[7]=8;

a[8]=9;

a[9]=10;

System.out.println("The element at a[5] is: "+a[5]);

}

}

Output

Storing the numbers in array

The element at a[5] is: 6

Program explanation

In above program we have created an array of 10 elements and stored some numbers in that array using the array index. The array that we have created in above program virtually should

look like this -

The System.out.println statement is for printing the value present at a[5]. We can modify the above program a little bit and i.e instead of writing

int a[];

a=new int[10];

these two separate statements we can combine them together and write it as -

int a[]=new int[10];

That means the declaration and initialization is done simultaneously.

Another way of initialization of array is

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

That means, as many number of elements that are present in the curly brackets, that will be the size of an array. In other words there are total 10 elements in the array and hence size of array will be 10.

Two Dimensional Arrays

• The two dimensional arrays are the arrays in which elements are stored in rows as well as in columns.

•For example

• The two dimensional array can be declared and initialized as follows

Syntax

data_type array_name=new data_type[size];

For example

int a new int[10];

Let us straight away write a Java program that is using two dimensional arrays -

Java Program [Sample2DArray.java]

/*

This is a Java program which makes use of 2D arrays

*/

class Sample2DArray

{

public static void main(String args[])

{

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

int k=0;

System.out.println("\tStoring the numbers in array");

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

{

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

{

a[i][j]=k+10;

k=k+10;

}

}

System.out.println("You have stored...");

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

{

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

{

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

}

System.out.println();

}

}

}

Output

Storing the numbers in array

You have stored...

10 20 30

40 50 60

70 80 90

The typical application of two dimensional arrays is performing matrix operations. Let have some simple Java program in which various matrix operations are performed.

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : with Example Java Programs - Arrays