如何打印出 class 个对象的 ArrayList?

How do I print out an ArrayList of class objects?

所以我正在尝试为以下 class 编写一个 getter 方法:

public class Birthday {
    private String bdayKid;
    private int age;
    private boolean gift;
    
    public Birthday(String bdayKid, int age, boolean gift) {
        this.bdayKid = bdayKid;
        this.age = age;
        this.gift = gift;
    }
    
    private ArrayList<Birthday> bdays = new ArrayList<Birthday>();
}

这是一个简单的 class,变量不复杂,但我想我在编写方法时迷路了 :( 到目前为止,我有以下内容:

public ArrayList<Birthday> getBirthdays() {
        Birthday bdays;        
        Iterator<Birthday> it = bdays.iterator();

        for (Birthday bday : bdays) {
            while (it.hasNext()) {
                bdays += it.next();
            }
        return bdays;
        }
    }

我正在尝试(!)做的是最终打印出 ArrayList,但老实说,我只是很困惑。因此,如果有人可以帮助我或告诉我哪里出了问题,我将不胜感激!
我收到的错误消息之一是 return bdays;:“不兼容的类型:生日无法转换为 java.util.ArrayList”

老实说,我有点困惑。

a) 那么让我们来看看你想要打印的情况(控制台):

覆盖 toString() 方法,这样您就可以按照自己的方式打印生日。

public void getBirthdays() {
      for (Birthday bday : bdays) {
         System.out.println(bday);
      }
}

b) 你想要 return bdays arraylist 的情况:

public ArrayList<Birthday> getBirthdays() {
    return this.bdays;
}

希望对您有所帮助。 :)

public class Main {

public static void main(String[] args) {
    Birthday danny = new Birthday("danny","22-10-2011", 10, true);
    Birthday ryan = new Birthday("ryan", "10-05-2013", 8, false);
    ArrayList<Birthday> birthdaysKids = new ArrayList<>();
    birthdaysKids.add(danny);
    birthdaysKids.add(ryan);

    for (Birthday b : birthdaysKids) {
        System.out.println(b.name + "'s birthday is on " + b.bdayKid + " and he is doing " + b.age +
                          " so, "+ (b.gift ? "he deserver a present" : "he does not deserve nothing"));
    }

输出:

danny 的生日是 22-10-2011,他快 10 岁了,他应该得到一份礼物

ryan 的生日是 10-05-2013,他已经 8 岁了,他什么都不配

}

static class Birthday {
    private String name;
    private String bdayKid;
    private int age;
    private boolean gift;

    public Birthday(String name, String bdayKid, int age, boolean gift) {
        this.name = name;
        this.bdayKid = bdayKid;
        this.age = age;
        this.gift = gift;
    }

    public String getBdayKid() {
        return bdayKid;
    }

    public void setBdayKid(String bdayKid) {
        this.bdayKid = bdayKid;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isGift() {
        return gift;
    }

    public void setGift(boolean gift) {
        this.gift = gift;
    }
}

}

您的 Birthday class 结构非常适合 创建 生日。生日列表 private ArrayList<Birthday> bdays = new ArrayList<Birthday>(); 应该放在 Birthday class.

之外

Birthday.java

public class Birthday {
    private String bdayKid;
    private int age;
    private boolean gift;
    
    public Birthday(String bdayKid, int age, boolean gift) {
        this.bdayKid = bdayKid;
        this.age = age;
        this.gift = gift;
    }
}

启动程序时,您可以创建要打印的生日列表。

MyBirthdayProgram.java

public class MyBirthdayProgram{
   public static void main(String []args) {
      //get your list of birthdays that you want to print
      ArrayList<Birthday> bdays = getBirthdays();
      //print your list of birthdays
      printBirthdays(bdays);
   }

   //This method will return a list of birthdays
   public ArrayList<Birthday> getBirthdays() {     
      //initialize your list of birthdays so that you can add to it
      ArrayList<Birthday> bdays = new ArrayList<Birthday>();

      //create a birthday
      Birthday birthday1 = new Birthday("nasrot", 10, true);
      //remember to add the birthday to your list of birthdays
      bdays.add(birthday1);

      //create a birthday
      Birthday birthday2 = new Birthday("redwingsdan", 8, false);
      //remember to add the birthday to your list of birthdays
      bdays.add(birthday2);

      //remember to return your list of birthdays!
      return bdays;
   }

   //This method will print a list of birthdays
   public void printBirthdays(ArrayList<Birthday> bdays) {    
      //iterate through each birthday and print out the result 
      for (Birthday bday : bdays) {
         System.out.println(bday);
      }
   }
}

为了让 System.out.println(bday); 以您期望的方式工作,您应该按照亚里士多德的建议,通过覆盖 Birthday 中的 toString() 方法打印出您的生日 class.

这是一篇关于覆盖 toString() 方法的有用文章 https://www.geeksforgeeks.org/overriding-tostring-method-in-java/

Birthday.java

public class Birthday {
    private String bdayKid;
    private int age;
    private boolean gift;
    
    public Birthday(String bdayKid, int age, boolean gift) {
        this.bdayKid = bdayKid;
        this.age = age;
        this.gift = gift;
    }

    @Override
    public String toString() {
       return "Happy birthday to " + this.bdayKid + "! "
              + this.bdayKid + " is " + this.age + " years old. "
              + this.bdayKid + " is " + (this.gift ? "" : "not ") + " getting a gift.";
    }
}

您使用此代码的打印结果将是:

Happy birthday to nasrot! nasrot is 10 years old. nasrot is getting a gift.

Happy birthday to redwingsdan! redwingsdan is 8 years old. redwingsdan is not getting a gift.