0

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?

3
  • 3
    docs.oracle.com/javase/tutorial/java/concepts Commented Aug 28, 2013 at 13:14
  • 9
    Array is not the correct way to do what you want. You should create a class for Department, and Students. Commented Aug 28, 2013 at 13:14
  • 1
    Also go by the name of "jagged arrays". Commented Aug 28, 2013 at 13:18

3 Answers 3

1
int college[][][] = new int[3];
college[0] = new int[5];
college[1] = new int[6];
...
college[0][0] = new int[count_marks_dept1_student1];
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.