JAVA ARRAYS
Array is a collection of similar data type values or in other words array is a collection of those values which are related with same data type.
So now the question arise, why we need the array?
Suppose we want to insert marks for class XII students and total 50 students are there.
Every student require marks for five subjects.
So we need to declare (50 x 5 = 250) variables that need several lines of code but by using of array we can do this task by using only one variable.
Every student require marks for five subjects.
So we need to declare (50 x 5 = 250) variables that need several lines of code but by using of array we can do this task by using only one variable.
FEATURES OF ARRAY
1. All the values store in one variable by help of different index values.
1. All the values store in one variable by help of different index values.
2. All the values of array related with same data type.
3. In array values stores in a contiguous memory location.
4. Array allows random access to its values with help of index value.
5. Array has fixed size, so it normally cause to overflow and underflow.
HOW TO DECLARE ARRAY IN JAVA
int marks [ ]={23, 45, 65, 30, 40};
In the above example 'int' is a data type and 'marks' is name of array variable.
The array variable 'marks' initialize with five integer values.
JAVA ARRAY EXAMPLE
class array
{
public static void main(String args[])
{
int marks[]={23,45,43,34,67};
int i,tot=0;
//Loop to print array values
for(i=0; i<5; i++)
{
System.out.println(marks[i]);
tot = tot + marks[i];
}
System.out.println("Total of marks is: "+tot);
}
}
5. Array has fixed size, so it normally cause to overflow and underflow.
HOW TO DECLARE ARRAY IN JAVA
int marks [ ]={23, 45, 65, 30, 40};
In the above example 'int' is a data type and 'marks' is name of array variable.
The array variable 'marks' initialize with five integer values.
JAVA ARRAY EXAMPLE
class array
{
public static void main(String args[])
{
int marks[]={23,45,43,34,67};
int i,tot=0;
//Loop to print array values
for(i=0; i<5; i++)
{
System.out.println(marks[i]);
tot = tot + marks[i];
}
System.out.println("Total of marks is: "+tot);
}
}
- - - PROGRAM OUTPUT - - -
0 Comments