繁体   English   中英

Java中具有不同长度的数组的数组

[英]arrays of arrays with different length in java

我的问题是,我们可以以此热情声明数组吗?

int college[][][];

它包含3个模块,分别是部门,学生,标记

我需要一个系5个学生,另一个系6个学生

我们可以声明这样的数组吗? 如果可以,怎么办?

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

您可以做到这一点,但是您应该问自己是否不应该使用面向对象的编程技术。

例如:

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

如果您确实要将其存储在3D阵列中,则可以对2个部门进行以下操作:

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

但这是在DepartmentStudent单独班级中处理此问题的更好的方法。 是否有特殊要求,为什么需要在数组中进行处理?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM