• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Java Arrays

Java Exception Handling





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

1.One 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

MSK TechnologiesTrainings







1.Array initialization

It means initilize the some values to the array.

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

Output:
Total marks are : 60
2.Array Declaration

In the array declaration no values initilize to the array

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

Output:
Enter subject 1 marks : 12
Enter subject 2 marks : 12
Enter subject 3 marks : 12
Enter subject 4 marks : 12
Enter subject 5 marks : 12
Total Marks Are 60
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

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

Output:
Total Marks are :60
Multi Dimensional Arrays

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

Multi Dimensional Array initialization

It means initilize the some values to the array.

Syntax:
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;
        }
    }
}                     

Output:
Student 1 Marks are : 5
Student 2 Marks are : 10
Student 3 Marks are : 15
Multi Dimensional Array Declaration

In the Multi Dimensional array declaration no values initilize to the array

Syntax:
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;
        }
    }
}                            

Output:
Student 1 subject 1 Marks : 1
Student 1 subject 2 Marks : 1
Student 1 subject 3 Marks : 1
Student 1 subject 4 Marks : 1
Student 1 subject 5 Marks : 1
Student 1 Marks are : 5

Student 2 subject 1 Marks : 2
Student 2 subject 2 Marks : 2
Student 2 subject 3 Marks : 2
Student 2 subject 4 Marks : 2
Student 2 subject 5 Marks : 2
Student 2 Marks are : 10

Student 3 subject 1 Marks : 3
Student 3 subject 2 Marks : 3
Student 3 subject 3 Marks : 3
Student 3 subject 4 Marks : 3
Student 3 subject 5 Marks : 3
Student 3 Marks are : 15
Multi Dimensional Array Assignment

In the Multi Dimensional 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

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

Output:
Student 1 Marks are : 5
Student 2 Marks are : 10
Student 3 Marks are : 15