PIJ Array Converted 1
PIJ Array Converted 1
ARRAY
class Problem
{
public static void main(String args[])
{
int x=0;
x=5;
x=10;
System.out.println(“X= ” + x);
}
}
This very example will definitely print the value of x as 10. Why
so? Because when a value 10 is assigned to x, the earlier value
of x, i.e. 5, is overlapped by new value. Thus the ordinary
variables we have used so far is only capable of holding one
value at a time. However, in some situations we would want to
store more than one value at a time in a single variable.
For example, suppose we wish to arrange the percentage marks
obtained by 100 students, we have two options to store these
marks in memory:
a. Construct 100 variables to store percentage marks
obtained by 100 different students.
b. Construct one variable (array) capable of storing or holding
all the hundred values.
1
Programming in JAVA by Roly Sinha
Definition:
Advantages:-
• Variables of same type can be stored together without
declaring separate variable for each.
• Accessing, deletion and insertion in array is fast and easy.
2
Programming in JAVA by Roly Sinha
Disadvantages:-
• The memory location is limited; we cannot increase or
decrease the space at run time.
Types of Array:
3
Programming in JAVA by Roly Sinha
array_name=new type[size];
int a[];//declaration
a=new int[5];//creation 5*4=20 bytes continuous
Examples:-
number=new int[5];
average=new float[10];
for(i=0;i<5;i++)
arr[i]= sc.nextInt();
5
Programming in JAVA by Roly Sinha