// Java program to illustrate creating an array of objects
class Student
{
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of array are objects of a class Student.
public class StudentDemo
{
public static void main (String[] args)
{
// declares an Array of Student Object.
Student[] arr;
// allocating memory for 5 objects of type Student.
arr = new Student[5];
// initialize the first elements of the array
arr[0] = new Student(1,"govind");
// initialize the second elements of the array
arr[1] = new Student(2,"ajit");
// so on...
arr[2] = new Student(3,"mani");
arr[3] = new Student(4,"dinesh");
arr[4] = new Student(5,"sanjay");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}
class Student
{
public int roll_no;
public String name;
Student(int roll_no, String name)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of array are objects of a class Student.
public class StudentDemo
{
public static void main (String[] args)
{
// declares an Array of Student Object.
Student[] arr;
// allocating memory for 5 objects of type Student.
arr = new Student[5];
// initialize the first elements of the array
arr[0] = new Student(1,"govind");
// initialize the second elements of the array
arr[1] = new Student(2,"ajit");
// so on...
arr[2] = new Student(3,"mani");
arr[3] = new Student(4,"dinesh");
arr[4] = new Student(5,"sanjay");
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}
No comments:
Post a Comment