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);
}
}

Share with SociBook.com

Random Posts

  1. 1 Comment(s)

  2. By carl on Jan 31, 2011 | Reply

    i want my program to allow more than one trainee’s marks to be entered, and then print the results for each trainee at the
    end of the program, rather than after each trainee’s marks have been entered.i got stuck and need your help

    import java.util.Scanner;
    public class InstructionalModulesResult {
    private static int firstint,intModuleQuizMark, noOfQuiz;
    private static String lastString, fistString,stringMode;
    private static double [] QuizMark;
    static char letterGrade;
    public static void main(String[] args) {
    boolean vinto=true;
    String ans;
    int tNum=0, nOfQuiz;
    double [] quizMark;
    double finalModule, avgMark, sumMark=0, overallMark;
    String famName, firName, instModule;
    char markGrade;
    boolean found=false;
    Scanner in = new Scanner(System.in);
    Scanner inStr = new Scanner(System.in);
    for(int i=1;(!found);i++)
    {
    System.out.print(“Trainee Number: “);
    tNum=in.nextInt();
    if ((tNum=9999))
    System.out.println(“Invalid Number – Please re-enter “);
    else
    found=true;
    }
    System.out.print(“Family Name: “);
    famName =inStr.nextLine();
    System.out.print(“First Name: “);
    firName =inStr.nextLine();
    System.out.print(“Instruction Module: “);
    instModule =inStr.nextLine();
    System.out.print(“How many quizes shall i calculate?: “);
    nOfQuiz =in.nextInt();
    quizMark = new double [nOfQuiz];
    for(int k=0; k<nOfQuiz;k++)
    {
    System.out.print("Module Quiz mark "+(k+1)+": ");
    tNum=in.nextInt();
    if ((tNum=100))
    System.out.println(“invalid Quiz mark, please re-enter a valid quiz mark not above 100″);
    else
    found=true;
    quizMark[k]=in.nextDouble();
    }
    System.out.print(“Final Module Test mark: “);
    finalModule = in.nextDouble();

    System.out.print(“Another [Y/N]?”);
    ans=in.nextLine();
    if(ans==”Y”)vinto=true;

    else
    vinto=false;
    {
    for (int k=0; k=70)
    letterGrade = ‘A’;
    else if (graded>=60)
    letterGrade = ‘B’;
    else if (graded>=50)
    letterGrade = ‘C’;
    else if (graded>=40)
    letterGrade = ‘D’;
    else if (graded<=39 )
    letterGrade = 'E';
    return letterGrade;

    }
    }

Post a Comment