【问题标题】:How to properly display contents of a shared array within objects如何在对象中正确显示共享数组的内容
【发布时间】:2014-05-04 19:15:59
【问题描述】:

我很难弄清楚如何使用适当的课程对象显示共享的 personArray。例如,我需要显示一门课程的列表以及该课程中的人员,并再次显示下一门课程。我觉得我的嵌套 for 循环或 personCount 变量有问题,因为并非每门课程的人数都相同,我该如何处理?

希望大家可以清楚地了解下面的精简代码。

注意*我必须使用aggregation和toStrings,不允许使用lists、arraylists或scanner。

谢谢!

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();
    } 
}

【问题讨论】:

    标签: java arrays object printing tostring


    【解决方案1】:

    确保你的 personArray 中的所有 50 个人都首先被初始化;通过像这样打印出整个数组来检查它们是否是这样(您需要导入 java.util.Arrays):

    System.out.println(Arrays.toString(course[x].personArray));
    

    如果返回任何“null”值,那么您就知道,并非 PersonArray 中的每个人都首先被初始化。

    【讨论】:

    • 我已经尝试了所有建议的答案,但每次 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
    • 啊解决了!我只需要在 for 循环下放一个简单的 if (schoolArray[x].personArray[y]!=null)
    【解决方案2】:

    您应该将 courseCount 更改为 courseArray.length 并将 personCount 更改为 courseArray.getPersonArraySize() 并在 Course 类中实现 getPersonArraySize() 以返回 personArray 的长度。

    【讨论】:

    • 我在 School 类中设置了 personCount 和 courseCount 两个静态值,也尝试了这个,但它仍然给我相同的空指针?!
    • 它们不能是静态的。您应该替换 for 循环中的 courseCount 和 personCount 并删除静态变量。你能做到吗?
    • 是的,我用 .length 尝试过,没有静态变量
    【解决方案3】:

    用下面的这些替换你的循环..

    for (Course c : courseArray) {
    
       print += c.getCourseName() + "\n";
    
       for (Person p : c.personArray){
    
         print += p.getPersonName()+ "\n";
    
        }
    }
    

    【讨论】:

    • 我仍然在 "print += p.getPersonName()+ "\n";" 这一行得到一个空指针??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多