Java 如何在对象中正确显示共享数组的内容
我很难弄清楚如何用适当的课程对象显示共享personArray。例如,我需要显示一个课程列表以及该课程中的人员,然后再显示下一个课程的人员列表。我觉得我的嵌套for循环或personCount变量有问题,因为不是每个课程都有相同数量的人,所以我该如何处理这个问题 希望你们都能清楚地了解下面的代码 注意*我必须使用聚合和ToString,不允许使用列表、ArrayList或扫描程序 谢谢Java 如何在对象中正确显示共享数组的内容,java,arrays,object,printing,tostring,Java,Arrays,Object,Printing,Tostring,我很难弄清楚如何用适当的课程对象显示共享personArray。例如,我需要显示一个课程列表以及该课程中的人员,然后再显示下一个课程的人员列表。我觉得我的嵌套for循环或personCount变量有问题,因为不是每个课程都有相同数量的人,所以我该如何处理这个问题 希望你们都能清楚地了解下面的代码 注意*我必须使用聚合和ToString,不允许使用列表、ArrayList或扫描程序 谢谢 public class School { private static Course[] cour
public class School {
private static Course[] courseArray = new Course[5];
public static void printList(){
String print = "";
for (int x = 0; x < courseCount; x++) {
print += courseArray[x].getCourseName() + "\n";
for (int y = 0; y < personCount; y++){
print += courseArray[y].personArray[y].getPersonName()+ "\n";
//^ I thought I could use the 1st forloop index to stay in the 1 course and print out all the people for that course(like the hardcode example below) but I get a nullpointer
}
}
JOptionPane.showMessageDialog(null,print);
/*
If I hardcode the print String like below I get the correct output.
So I know my objects are being stored properly, but I can't figure out how to get the correct display with a loop
print += courseArray[0].personArray[0].getPersonName(); //tim (bio)
print += courseArray[0].peopleArray[1].getPersonName(); //bob (bio)
print += courseArray[1].peopleArray[2].getPersonName(); //john (art)
*/
}
}
------------------------
public class Course {
private Person[] personArray = new Person[50];
//constructor for course
// get/set courseName methods
public String toString(){
// not sure how to use this the right way
}
}
-----------------------------------
public class Person extends Course {
//constructor for person
// get/set personName methods
public String toString(){
return getPersonName();
}
}
公立学校{
私人静态课程[]Coursarray=新课程[5];
公共静态void打印列表(){
字符串打印=”;
对于(int x=0;x
确保首先初始化personArray中的所有50个人;通过这样打印整个数组来检查它们是否正确(您需要导入java.util.array):
如果返回任何“null”值,则您知道并非PersonArray中的每个人都是首先初始化的。您应该将courseCount更改为CourserRay.length,将personCount更改为CourserRay.getPersonArraySize(),并实现getPersonArraySize()在课程中返回personArray的长度。用以下循环替换您的循环
for (Course c : courseArray) {
print += c.getCourseName() + "\n";
for (Person p : c.personArray){
print += p.getPersonName()+ "\n";
}
}
我仍然在“print+=p.getPersonName()+“\n”;”行上得到一个空指针?我已经尝试了所有建议的答案,并且每次当x和y索引值不同时,我仍然得到一个空指针?我想我发现了问题;我会修改这个答案。system.out.println应该在forloop中吗?那么如何初始化所有50个人呢?因为我只做了“private Person[]personArray=new Person[50];”不,System.out.println可以在方法的最开始,甚至在String print=“”;”之前;。要初始化所有50个人(至少要测试程序),请使用构造函数创建一个for循环来创建新的Person对象。类似于“for(int i=0;i<50;i++){Person testPerson=new Person(“Bob”);/*或者根据构造函数这里包含的任何内容*/personArray[i]=testPerson;}”(为了清晰起见,引用)。该循环可以放在课程的构造函数中,至少用于测试目的,以消除NullPointerException。啊,解决了它!我只需要在for循环下放置一个简单的if(schoolArray[x].personArray[y]!=null)我已经在学校类中设置了personCount和courseCount这两个静态值,并且也尝试了这个方法,它仍然给我相同的null指针?!它们不能是静态的。您应该在for循环中替换courseCount和personCount,并删除静态变量。你能做到吗?是的,我试过用.length,没有静态变量
for (Course c : courseArray) {
print += c.getCourseName() + "\n";
for (Person p : c.personArray){
print += p.getPersonName()+ "\n";
}
}