如何打印类对象的ArrayList? [英] How do I print out an ArrayList of class objects?

查看:112
本文介绍了如何打印类对象的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试为以下类编写一个getter方法:

So I'm trying to write a getter method for the following class:

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

这是一个简单的类,具有简单的变量,但是我认为我迷失了编写方法的方法:(到目前为止,我有以下内容:

it's a simple class with uncomplicated variables, but I think I got lost writing the method :( So far I have the following:

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"

what I'm TRYING(!) to do is to print out the ArrayList eventually but honestly I just got so confused. So if anybody could help me or tell me where I went wrong I'd be super grateful!
One of the error messages I'm receiving is for return bdays;: "incompatible types: Birthday cannot be converted to java.util.ArrayList"

推荐答案

您的 Birthday 类结构对于创建生日非常有用.生日列表 private ArrayList< Birthday>bdays = new ArrayList< Birthday>(); 是应该放在 Birthday 类之外的东西.

Your Birthday class structure is good for creating birthdays. The list of birthdays private ArrayList<Birthday> bdays = new ArrayList<Birthday>(); is something that should be placed outside of your 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;
    }
}

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

When you start your program, you can create your list of birthdays that you want to print.

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); 以您期望的方式工作,您应该执行亚里士多德的建议并通过覆盖 toString()来打印您的生日类中的方法.

In order to get System.out.println(bday); to work the way you expect, you should do what Aristotle suggested and print out your birthdays by overriding a toString() method in the Birthday class.

关于覆盖 toString()方法 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.";
    }
}

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

Your print result using this code would be:

nasrot生日快乐!nasrot是10岁.nasrot收到礼物了.

redwingsdan生日快乐!redwingsdan是8岁.redwingsdan没有收到礼物.

这篇关于如何打印类对象的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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