Rakesh Kumar
Rakesh Kumar

Reputation: 1

arrays of arrays with different length in java

My Question is can we declare arrays in this passion

int college[][][];

it contains 3 blocks Departments, Students, Marks

I need 5 Students in one Department and 6 Students in another Department

Can we declares arrays like this. If so how?

Upvotes: 0

Views: 126

Answers (3)

Joni
Joni

Reputation: 111279

You can do this, but you should ask yourself if you shouldn't be using object-oriented programming techniques instead.

For example:

int departments = 5; // 5 departments
int[][][] college = new int[departments][][];

int students = 20; // 20 students in first department
college[0] = new int[students][];

int marks = 10; // 10 marks for first student in first department
college[0][0] = new int[marks];
college[0][0][0] = 3; // first mark for first student in first department

students = 17; // 17 students in second
college[1] = new int[students][];

// and so on...

Upvotes: 0

user1983983
user1983983

Reputation: 4841

If you really want to store this in a 3D-array you can do it the following way for 2 departments:

int college[][][] = new int[] {new int[5], new int[6]}

But it would be a far better approach to handle this in separate classes for Department and Student. Is there a special requirement why you need to handle this in arrays?

Upvotes: 0

hpid91
hpid91

Reputation: 113

int college[][][] = new int[3];
college[0] = new int[5];
college[1] = new int[6];
...
college[0][0] = new int[count_marks_dept1_student1];

Upvotes: 1

Related Questions