Arrays

Arrays

Array is a collection of similar data or homogeneous data.In Java langueage these Arrays are classified into following types.
1.One Dimensional Arrays
2.Multi Dimensional Arrays

1.One Dimensional Arrays

One Dimensional Arrays means only single row. ie single student multiple subjects.
Array defination types
1.Array Initilization
2.Array Declaration
3.Array Assignment

1.Array Initilization
It means initilize the some values to the array.
Syntax
datatype arrayname[]={value1,value2,........}
in the above syntax multiple values are stored in single arrayname.
class ArrayInit
{
public static void main(String args[])
{
int sum=0,x,n;
int marks[]={12,1,2,12,12,1,2,12,1,21,12};
n=marks.length;
for(x=0;x<n;x++)
{
sum=sum+marks[x];
}
System.out.println("Total marks are "+sum);
}
}
    
2.Array Declaration
In the array declaration no values initilize to the array
Syntax
datatype arrayname[]=new datatype[size];
import java.util.*;
class ArrayDeclaration
{
public static void main(String args[])
{
int sum=0,x,n;
int marks[]=new int[5];
n=marks.length;
Scanner s=new Scanner(System.in);
for(x=0;x<n;x++)
{
System.out.println("Enter subject "+(x+1)+" marks");
marks[x]=s.nextInt();
sum=sum+marks[x];
}
System.out.println("Total Marks Are "+sum);
}
}
 
3.Array Assignment
In the array Assignment assign the values initilize to the array
Syntax
datatype arrayname[]=new datatype[size]; //Declaration
arrayname[index]=value; //Assign the values to the array
    
class ArrayAssignmnet
{
public static void main(String args[])
{
int sum=0,x,n;
int marks[]=new int[5];
marks[0]=12;
marks[1]=12;
marks[2]=12;
marks[3]=12;
marks[4]=12;
n=marks.length;
for(x=0;x<n;x++)
{

sum=sum+marks[x];
}
System.out.println("Total Marks are "+sum);
}
}


Multi Dimensional Arrays

Multi Dimensional Arrays means rows and columns ie multiple students multiple subjects
These are classified into floowing types
Array Initilization
Array Declaration
Array Assignment

Array Initilization
It means initilize the some values to the array.
Syntax
datatype arrayname[rowsize][colsize]={{value1,value2,...},{value1,value2,..},......}

in the above syntax multiple values are stored in single arrayname.
class MultiDimArr
{
public static void main(String args[])
{
int sum=0,x,y;
int marks[][]={{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3}};

for(x=0;x<3;x++)
{
for(y=0;y<5;y++)
{
sum=sum+marks[x][y];
}
System.out.println("Student "+(x+1)+" Marks are "+sum);
sum=0;
}
}
}
    
Array Declaration

In the array declaration no values initilize to the array
Syntax datatype arrayname[][]=new datatype[rowsize][colsize];
    
import java.util.*;
class MultiArrDec
{
public static void main(String args[])
{
int sum=0,x,y;
int marks[][]=new int[3][5];
Scanner s=new Scanner(System.in);
for(x=0;x<3;x++)
{
for(y=0;y<5;y++)
{
System.out.println("Student "+(x+1)+" subject "+(y+1)+" Marks are");
marks[x][y]=s.nextInt();
sum=sum+marks[x][y];
}
System.out.println("Student "+(x+1)+" Marks are "+sum);
sum=0;
}
}
}
Array Assignment
In the array Assignment assign the values initilize to the array
Syntax
datatype arrayname[][]=new datatype[rowsize][colsize]; //Declaration
arrayname[rowindex][colindex]=value; //Assign the values to the array
class MultiArrAssi
{
public static void main(String args[])
{
int sum=0,x,y;
int marks[][]=new int[3][5];
marks[0][0]=12;
marks[0][1]=12;
marks[0][2]=12;
marks[0][3]=12;
marks[0][4]=12;
marks[1][0]=12;
marks[1][1]=12;
marks[1][2]=12;
marks[1][3]=12;
marks[1][4]=12;
marks[2][0]=12;
marks[2][1]=12;
marks[2][2]=12;
marks[2][3]=12;
marks[2][4]=12;
for(x=0;x<3;x++)
{
for(y=0;y<5;y++)
{

sum=sum+marks[x][y];
}
System.out.println("Student "+(x+1)+" Marks are "+sum);
sum=0;
}
}
}