Menu
Topics Index
...
`


Array - Overview >
Siva Nookala - 11 Apr 2016
The program shows how to print details of multiple students using arrays.

Print student details using arrays
class PrintStudentDetailsUsingArrays
{
    public static void main(String arg[])
    {
        String names[] = { "Rajesh", "Suresh", "Ramesh", "Kamlesh", "Vignesh" };
        int marks[] = { 45, 78, 83, 77, 93 };
        char sections[] = { 'A', 'B', 'A', 'A', 'B' };
        
        for(int i = 0; i < names.length; i++)
        {
            System.out.println( names[i] + " in section " + sections[i] + " got " + marks[i] + " marks." );
        }    
    }
}
OUTPUT

Rajesh in section A got 45 marks.
Suresh in section B got 78 marks.
Ramesh in section A got 83 marks.
Kamlesh in section A got 77 marks.
Vignesh in section B got 93 marks.

DESCRIPTION

Here we have created arrays names, marks and sections to store the student details. We used a for loop to iterate through the elements and print the details. The lengths of the all the arrays are same, hence the first element (index 0) in names correspond to first element in sections and marks array. Similarly the second element (index 1) in names correspond to second element in sections and marks array. So if we want Ramesh details we need to look at 3rd element (index 2) in every array.

THINGS TO TRY
  • Add the details of another student Venkatesh in section B with marks 87 and see the output.
  • Remove the details of Ramesh i.e. remove "Ramesh" from names array, remove 83 from marks array and remove the second 'A' in sections array and see the output.
  • Remove the name of student Suresh from names array, but keep his marks and sections and see the output. Observe that the output does not print the marks and sections correctly.
  • Remove the marks from marks array and section from sections array for the student Kamlesh, but keep his name in the names array. When we do this it throws an ArrayIndexOutOfBoundsException since we do not have the section and name corresponding to the student.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.