Opening Hours :7AM to 9PM
Array is a collection of similar data or homogeneous data.In Java language these Arrays are classified into following types.
1. One Dimensional Arrays
2. Multi Dimensional Arrays
One Dimensional Arrays means only single row. ie single student multiple subjects.
Array definition types
1. Array initialization
2. Array Declaration
3. Array Assignment
It means initilize the some values to the array.
datatype arrayname[ ]={value1,value2,value3,........}in the above syntax multiple values are stored in single arrayname.
public class Main { public static void main(String args[]) { int sum=0,x,n; int marks[ ]={12,12,12,12,12}; n=marks.length; for(x=0;x<n;x++) { sum=sum+marks[x]; } System.out.println("Total marks are: "+sum); } }
In the array declaration no values initilize to the array
datatype arrayname[ ]=new datatype[size];
import java.util.*; public class Main { 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); } }
In the array Assignment assign the values initilize to the array
datatype arrayname[]=new datatype[size]; //Declaration arrayname[index]=value; //Assign the values to the array
public class Main
{ 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 means rows and columns(multiple students multiple subjects)
These are classified into following types
1. Multi Dimensional Array initialization
2. Multi Dimensional Array Declaration
3. Multi Dimensional Array Assignment
It means initilize the some values to the array.
datatype arrayname[ ][ ]={{value1,value2,...},{value1,value2,..},......}in the above syntax multiple values are stored in single arrayname.
public class Main { 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; } } }
In the Multi Dimensional array declaration no values initilize to the array
datatype arrayname[ ][ ]=new datatype[rowsize][colsize];
import java.util.*; public class Main { 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 :"); marks[x][y]=s.nextInt(); sum=sum+marks[x][y]; } System.out.println("Student "+(x+1)+" Marks are : "+sum); sum=0; } } }
In the Multi Dimensional array Assignment assign the values initilize to the array
datatype arrayname[ ][ ]=new datatype[rowsize][colsize]; //Declaration arrayname[rowindex][colindex]=value; //Assign the values to the array
public class Main { public static void main(String args[]) { int sum=0,x,y; int marks[][]=new int[3][5]; marks[0][0]=1; marks[0][1]=1; marks[0][2]=1; marks[0][3]=1; marks[0][4]=1; marks[1][0]=2; marks[1][1]=2; marks[1][2]=2; marks[1][3]=2; marks[1][4]=2; marks[2][0]=3; marks[2][1]=3; marks[2][2]=3; marks[2][3]=3; marks[2][4]=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; } } }