如何从2个不同的阵列打印内容,并有分组的项目 [英] How to print contents from 2 different arrays and have items grouped

查看:93
本文介绍了如何从2个不同的阵列打印内容,并有分组的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输出所有的俱乐部名单随着人们输入每个俱乐部,但它单独显示每个俱乐部(即有一家具乐部以及人们对于俱乐部的列表,然后第二个俱乐部输入和人民为一个等)

我想确保对象每个阵列的加入是正确的,并找出我的的toString()方法应该是什么样子。

下面是我的code迄今:

 公共类应用{    公共静态俱乐部[] = clubArray新东家[5];
    公共静态INT clubCount = 0;
    公共静态INT personCount = 0;    公共静态无效的主要(字串[] args){
        //添加一个方法内
        //为俱乐部提示用户
        clubArray [clubCount ++] =新的俱乐部(clubName);        对于名称//提示用户,然后提示男性或女性
        如果(x.equals(M)){
            男newPerson =新男(名);
            clubArray [clubCount-1] .addPerson(newPerson,personCount);
            personCount ++;
        }
    }    //数据定义类
    公共类俱乐部{//扩展应用程序?
        公共静态人[] = personArray新的Person [200];        公共无效addPerson的(人newPerson,诠释personCount){
            personArray [personCount] = newPerson;
        }
    }


解决方案

该personArray不应是静态的。如果是静态的所有俱乐部实例将共享的人是一样的阵列。这是更好地使用List,而不是静态数组。

 公共类俱乐部{
     字符串名称;
     清单<&人GT;人=新的LinkedList<>();
     市民俱乐部(字符串名称){this.name =名称; }     公共无效addPerson的(人P)
     {
       people.add(P);
     }     公众诠释countPeople()
     {
       返回people.size();
     }     公共字符串的toString()
     {
        StringBuilder的SB =新的StringBuilder(俱乐部的名字:)追加(名称)。
        对(人电话号码:人)
            sb.append(p)的.append();
        返回sb.toString();
     }
}

类人:

 公共类Person
{
   私人字符串名称;
   私人布尔男性;   公众人物(字符串名称,布尔男)
   {
      this.name =名称;
      this.male =男性;
   }   公共字符串的toString(){...}
}

类应用:

 公共类应用
{
    清单<俱乐部GT;俱乐部=新的ArrayList<>();    公共无效addClub(俱乐部俱乐部)
    {
       clubs.add(俱乐部);
    }    公共字符串的toString()
     {
        StringBuilder的SB =新的StringBuilder();
        对于(俱乐部C:俱乐部)
            sb.append(C);
        返回sb.toString();
     }    公共静态无效的主要(字串[] args)
    {
       应用程序=新的App();       俱乐部club1 =新东家(ClubName1);
       club1.add(新的Person(乔治,真正的));
       club1.add(新的Person(玛丽,FALSE));
       app.addClub(club1);       俱乐部club2 =新东家(ClubName2);
       club2.add(新的Person(雅各布,真正的));
       club2.add(新的Person(凯蒂,FALSE));
       app.addClub(club2);       的System.out.println(应用);    }
}

I'm trying to output a list of all the clubs along with the people entered for each club, but have it display each club individually (i.e. there is one club and the list of people for that club, then the second club entered and the people for that one and so on)

I wanted to make sure the adding of objects to each array was correct and figure out what my toString() method should look like.

Here's my code so far:

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++;
        }
    }

    //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;
        }
    }

解决方案

The personArray should not be static. If it is static all the Club instances will share the same array of people. It is better to use List instead of static arrays.

public class Club {
     String name;
     List<Person> people = new LinkedList<>();


     public Club(String name) { this.name = name; }         

     public void addPerson(Person p)
     { 
       people.add(p); 
     }         

     public int countPeople()
     {
       return people.size();
     }

     public String toString()
     {
        StringBuilder sb = new StringBuilder("Club name:").append(name);
        for(Person p : people)
            sb.append(p).append("  ");
        return sb.toString();
     }
} 

Class Person :

public class Person 
{
   private String name;
   private boolean male;

   public Person( String name, boolean male)
   {
      this.name = name;
      this.male = male;
   }

   public String toString() {  ... }
}

Class App :

public class App
{
    List<Club> clubs = new ArrayList<>();

    public void addClub(Club club)
    { 
       clubs.add(club);
    }

    public String toString()
     {
        StringBuilder sb = new StringBuilder();
        for(Club c : clubs)
            sb.append(c);
        return sb.toString();
     }

    public static void main(String[] args)
    { 
       App app = new App();

       Club club1 = new Club("ClubName1");
       club1.add(new Person("George", true));
       club1.add(new Person("Mary", false));
       app.addClub(club1);

       Club club2 = new Club("ClubName2");
       club2.add(new Person("Jacob", true));
       club2.add(new Person("Katie", false));
       app.addClub(club2);

       System.out.println(app);

    }
}

这篇关于如何从2个不同的阵列打印内容,并有分组的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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