【发布时间】:2016-10-16 23:07:31
【问题描述】:
我试图用字符串和整数创建一个 (2)2d 数组,以便稍后在代码中对它们进行排序。到目前为止,这是我的代码,我在尝试编译时不断出错,我不确定该怎么做:
public class SchoolBus {
public static void main(String[] args) {
Student[][] students = new Student[6][3];
students[0][0] = "Mabel";
students[0][1] = new int[1];
students[0][2] = new Integer(2);
students[1][0] = "Dipper";
students[1][1] = new Integer(1);
students[1][2] = new Integer(4);
students[2][0] = "Sam";
students[2][1] = new Integer(1);
students[2][2] = new Integer(7);
students[3][0] = "Ian";
students[3][1] = new Integer(2);
students[3][2] = new Integer(3);
students[4][0] = "Jordan";
students[4][1] = new Integer(2);
students[4][2] = new Integer(6);
students[5][0] = "Steven";
students[5][1] = new Integer(2);
students[5][2] = new Integer(9);
System.out.println(student);
}
}
【问题讨论】:
-
它有是一个数组吗?有更好的数据结构。您是否希望我们回答“我应该如何最好地存储相关整数和字符串的集合?”这个问题?
-
另外,您似乎在某处定义了一个类
Student。看到这一点会很有帮助,这样我们就可以为您创建更好的答案。 -
你的数组类型是
Student,所以它就是这样。 -
您还试图打印一个不存在的变量;学生。也许你忘记了最后的“s”?虽然 java.util.Arrays 有打印数组的字符串表示的方法,否则您将看不到它的内容,只能看到一些内部数组标识符。
-
我不认为当前的代码接近实际想要的。最终产品可能没有数组(它可能有
Set或Students)。当我们看到实际需要什么时,我们可以提出答案,让 OP 尽快达到他想要的状态。
标签: java arrays sorting multidimensional-array