Home | | Sitemap ||Page number :28

Arrays - A collection of Similar Data Type

Introduction

The concept of several data type and String has already been taught to you. They provide us with a lot of flexibility to work with any type of data. You might be wondering if you have mastered everything, but surely this is a wrong supposition until you have worked with Arrays. Soon you will realize that Arrays are one of the key features of Java.

Create a program that finds the largest number between the three numbers entered. You will surely be able to do it. Now picture yourself in a condition where the numbers of variables entered are also unknown, now you will get stuck here. To write such a program you need to learn arrays.

Array

An Array is a collection of similar type of finite numbers of data values, stored in memory location. An array of integers, characters, Strings as well as objects can be created. There are two types of Arrays —

  1. One-Dimensional
  2. Multi-dimensional

One-Dimensional

Have a look at the declaration of a one-dimensional array. Syntax— int arr []=new int[2]; //Declaration of an Array that can hold 2 variables OR

int []arr=new int[2]; An array is said to be single or multi-dimensional (two, three) by the number of subscripts. An array with a single subscript always has one square bracket. So how is array able to store multiple values of any kind of data? Here is the answer… int arr[0]=3; int arr[1]=4 The different values are stored in indexes that begin with zero. Going through the internal working you will find that our temporary memory is allocated serial wise in a continuous way.

0 1 2 3 4 5 6 7 8

23

You can work over these individual values easily.

Remember: Index of arrays always begin with zero.

Multi-Dimensional

A multidimensional array has more than one subscript. Arrays with two and three subscripts are two and three dimensional arrays respectively. A multidimensional array stores in rows and columns and not simply serial wise.

2342

In this picture we can see a double-dimensional array (3*3). The values are stored in this

way-
Other indexes are empty. Row 0 0 1 2 Column Value 0 3 1 9 2 6 0 7

Declaration for this multidimensional array is -- int ar[][]=new int[3][3];

Working with Arrays

Consider an Array int Arr[]=new int[5]; //Declaration

Initialization ---

We can initialize arrays by two ways- Arr [0]=1; Arr [1]=2; Arr [2]=3; Arr [3]=4; Arr [4]=5;

A more convenient way --- int Arr[]={1,2,3,4,5};

Input from the User

You will have to use InputStreamReader and BufferedReader classes to get input from the user. As an array might take hundreds of values, using loops (mostly ‘for’) to get the input is a good idea.

import java.io.*; class arr { public static void main(String args[]) throws IOException

{ int ar[]=new int[5]; // Array Declaration int c; InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);

for(c=0;c<5;c++) //a loop to take four inputs { System.out.println("Enter array elements"); String v1=inp.readLine(); ar[c]=Integer.parseInt(v1); //According to the value of ‘c’ the array gets initialized }

System.out.println("Printing details of the Array"); for(c=0;c<5;c++) { System.out.println(+ar[c]); } } }

234

Two Worked Out Examples

“Practice makes a student perfect!”, this statement is the ultimate truth in programming. Have a look at these examples and thereafter you will definitely understand Arrays better. Majority of the programs will be covered in the “Examples” section.

Sum of Array Elements

import java.io.*; class sum { public static void main(String args[]) throws IOException

{ int sum=0,lim,c; int arr[]=new int[50]; InputStreamReader rd=new InputStreamReader(System.in); BufferedReader inp=new BufferedReader(rd);

System.out.println("Enter the limit for numbers"); String v1=inp.readLine(); lim=Integer.parseInt(v1);

for(c=0;c<lim;c++) { System.out.println("Enter Array Elements"); String v2=inp.readLine(); arr[c]=Integer.parseInt(v2); }

for(c=0;c<lim;c++) { sum=sum+arr[c]; }

System.out.println("Sum of Array Elements is" +sum); } }

234

Finding Largest and Smallest number in an Array

class larsml { public static void main(String args[]) { int arr[]={8,5,4,3,9}; int c=0,lar=arr[c],sml=arr[c];

System.out.println("Processing....."); for(c=0;c<5;c++) {s if(arr[c]>lar) lar=arr[c]; if(arr[c]<sml) sml=arr[c]; } System.out.println("Largest No. is "+lar); System.out.println("Smallest No.is "+sml); } }

23

String Array

A program demonstrating an array of “String” class // Printing marks of Students import java.io.*; class record { public static void main(String args[]) throws IOException { String arr[]={"Nilay","Nitish","Nitin"}; int marks[]={97,92,90}; String inp; boolean fnd=false; // Initially initialized with false to demonstrate that our search has //not been successful int c;

InputStreamReader rd=new InputStreamReader(System.in); BufferedReader input=new BufferedReader(rd); System.out.println("Enter a Name"); inp=input.readLine();

for(c=0;c<3;c++) { if(inp.equalsIgnoreCase(arr[c])) { System.out.println("Marks are " +marks[c]); fnd=true; //The “Not Found!!!” message will not be printed in this case } }

if(fnd==false) System.out.println("Not Found!!!"); } }

23423