- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathObjectArray.java
44 lines (35 loc) · 1.22 KB
/
ObjectArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Java program to illustrate creating an array of
// objects
classStudent
{
publicintroll_no;
publicStringname;
Student(introll_no, Stringname)
{
this.roll_no = roll_no;
this.name = name;
}
}
// Elements of array are objects of a class Student.
publicclassGFG
{
publicstaticvoidmain (String[] args)
{
// declares an Array of integers.
Student[] arr;
// allocating memory for 5 objects of type Student.
arr = newStudent[5];
// initialize the first elements of the array
arr[0] = newStudent(1,"Atharv");
// initialize the second elements of the array
arr[1] = newStudent(2,"Vaibhav");
// so on...
arr[2] = newStudent(3,"Gargi");
arr[3] = newStudent(4,"Sunil");
arr[4] = newStudent(5,"Shikhar");
// accessing the elements of the specified array
for (inti = 0; i < arr.length; i++)
System.out.println("Element at " + i + " : " +
arr[i].roll_no +" "+ arr[i].name);
}
}