Define Array in Java



An Array is a group of similar data type items that share a common name. Arrays occupies contiguous memory locations. Array size is fixed and cannot be altered dynamically. For example, we can define an array name as salary to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called index number or subscript in brackets after the array name.

For example:

salary[3]

Represents the salary of 4th employee(array index starts from zero ). While the complete set of values are referred to as an array, the individual values are called elements. Arrays can be of any primitive data type.

One-dimensional Arrays :


A list of item can be given one variable name using only one subscript and such a variable is called as one-dimensional array.

Creating an Array :


Arrays must be declared and created in the computer memory before they are used. Creation of an array involves three steps;

(i) Declaring the array

(ii) Creating memory locations

(iii) Putting values in the memory locations

Declaration of Arrays :


Arrays in java may be declared in two forms :

1st way : datatype arrayname[];


2nd way : datatype[] arrayname;


Examples :


int number[];

float average[];

int[] counter;

float[] marks;


Note: The size of the array should not be mentioned while declaration


Creation of Arrays :


After declaring an array, memory should be allocated. Java allows the creation of arrays using new keyword, as shown below:

type arrayname=new type[size];


Examples:


int number[];

number=new int[5];

float average[];

average=new float[10];


This lines create necessary memory locations for the arrays number and average and designate them as int and float respectively. Now, the variable number refers to an array of 5 integers and average refers to an array of 10 floating point values.

It is also possible to combine both the steps– declaration and creation into aone as shown below.

float average = new float[10];

int num [] = new int[5];


Sample example of array’s in java are as follows;


//java program for Total marks and percentage.


import java.io.*;
class Arr
{
public static void main(String args[])throws IOException
{
//to accept data from keyboard

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“How many Subjects”);
int n=Integer.parseInt(br.readLine());


//create the array with size n
int marks[] = new int[n];

//store marks into an array marks[]
for(i=0;i<n;i++)
{
System.out.println(“enter marks”);
marks[i]=Integer.parseInt(br.readLine());
}

//display the marks again and final total
int tot=0;
for(int i=0;i<n;i++)
{
System.out.println(marks[i]);
tot=tot+marks[i];
}
System.out.println(“total marks=”+tot);

//final percentage of marks

float percent=(float)total/n;

System.out.println(“percentage=”+percent);
System.out.println(“arr size=”mark.length);
}
}

Random Posts

  • Java-Jdbc Questions
  • Life Cycle of Thread
  • Marking or Tagging Interface
  • Define JSP
  • Exception Handling and their uses

Post a Comment