使用的toString存储在不同的数组对象中打印出的对象(JAVA) [英] printing out objects within objects stored in different arrays using toString (java)

查看:121
本文介绍了使用的toString存储在不同的数组对象中打印出的对象(JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最困难的时候试图输出的所有俱乐部名单并具有谁在其下显示的俱乐部,但它会显示所有每个俱乐部来代替。我知道问题出嵌套的循环中,因为当它到达第二个循环,这是不言而喻的 personArray 用新的索引值,并打印出所有的人。我如何得到它,使它只打印 clubArray的内容[0] ,那么 clubArray [1] 它会拿起它在 personArray 和打印 clubArray [1]的人离开了吗?

 公共类应用{    公共静态俱乐部[] = clubArray新东家[5];
    公共静态INT clubCount = 0;
    公共静态INT personCount = 0;    公共静态无效的主要(字串[] args){
        //添加一个方法内
        //为俱乐部提示用户
        clubArray [clubCount ++] =新的俱乐部(clubName);        对于名称//提示用户,然后提示男性或女性
        如果(x.equals(M)){
            男newPerson =新男(名);
            clubArray [clubCount-1] .addPerson(newPerson,personCount);
            personCount ++;
        }           // ..在打印方法
        为(中间体X = 0; X&下; clubArray.length; X ++){
              显示器+ = clubArray [X] .getClubName();
              对于(INT Y = 0; Y< personCount; Y ++){
                显示器+ = clubArray [X]的ToString();
              }
            }    }    // --------------------------------
    //数据定义类    公共类俱乐部{//扩展应用程序?
        公共静态人[] = personArray新的Person [200];        公共无效addPerson的(人newPerson,诠释personCount){
            personArray [personCount] = newPerson;
        }    公共字符串的toString(){
    人personObj对象=新的Person();
    字符串显示=;
    返回显示= studentObj.getPersonName();    }
}


解决方案

personArray 俱乐部声明静态,这意味着只有一个人阵,而不是一个为各个俱乐部,这就是你presumably想(每个俱乐部拥有所有的人组成的数组在该俱乐部)。

这几乎肯定会是最好有俱乐部有一个列表<人> ,这样的名单可以成长的人加入到了俱乐部,而不是有200固定大小的(除非你知道,每家具乐部将始终具有完全相同200人在里面)。

I'm having the most difficult time trying to output a list of all clubs and have the people who are in that club displayed under it, but it displays all people for each club instead. I know the problem lies within the nested for loop because when it gets to the second for loop, it goes to the personArray with a new index value and prints out all the people. How do I get it so that it prints only the contents of clubArray[0], then for clubArray[1] it will pick up where it left off in the personArray and print clubArray[1]'s people?

public class app {

    public static Club[] clubArray = new Club[5];
    public static int clubCount=0;
    public static int personCount=0;

    public static void main(String[] args) {
        //inside a add method
        //prompt user for club 
        clubArray[clubCount++] = new Club(clubName);

        //prompt user for name, then prompt for Male or Female
        if (x.equals("M")) {
            Male newPerson = new Male(name);
            clubArray[clubCount-1].addPerson(newPerson,personCount);
            personCount++;
        }

           // .. in a print method
        for(int x = 0; x < clubArray.length; x++) {
              display+= clubArray[x].getClubName();
              for(int y = 0; y < personCount; y++) {
                display += clubArray[x].toString();
              }
            }

    }

    //--------------------------------
    //data definition class

    public class Club { //extend app?
        public static Person[] personArray = new Person[200]; 

        public void addPerson(Person newPerson, int personCount){
            personArray[personCount] = newPerson;
        }

    public String toString() {
    Person personObj = new Person();
    String display= "";
    return display = studentObj.getPersonName();

    }
}

解决方案

The personArray in Club is declared static, which means that there is only one person array, instead of one for each club, which is what you presumably want (each club has an array of all the people in that club).

It would almost certainly be better to have the club have a List<Person> so that the list can grow as people are added to the club, instead of having a fixed size of 200 (unless you know that every club will always have exactly 200 people in it).

这篇关于使用的toString存储在不同的数组对象中打印出的对象(JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆