How to initialize a custom data type array with more than 1 member variables?

advertisements

Arrays of Primitive data types can be initialized like :

int a[] = {1 , 2, 3, 4, 5};

I have created a custom data type with a code like this -

class Student
{
    private String name;
    private int rollno;
}

I have created a Student array as given below :

Student s[] = new Student[5];
for(int i =0; i < s.length; s++)
    s[i] = new Student();

I want to initialize the member variables : name and rollno without taking user input. How can I do that? I want to do something like -

Student s[] = {("Sam", 21), ("Jules",3)...}


Assuming there's such constructor Student(String, int), then you can try this:

Student[] studentArray = {
    new Student("Sam", 21),
    new Student("Jules",3)
};