Java 如何循环遍历对象的ArrayList并使其打印所有内容?
几天前我问了一个问题,现在我有一个更具体的问题,因为我在我的程序上做了更多的工作,并添加了一些东西。基本上,我从一个空的ArrayList开始,它应该保存通过控制台添加的生日。我记下的这部分+它打印我添加的生日。但是如果我想添加另一个,它会打印我再次添加的第一个?我怎样才能让它打印出迄今为止我添加的所有生日?我将显示到目前为止的代码 生日班Java 如何循环遍历对象的ArrayList并使其打印所有内容?,java,loops,arraylist,Java,Loops,Arraylist,几天前我问了一个问题,现在我有一个更具体的问题,因为我在我的程序上做了更多的工作,并添加了一些东西。基本上,我从一个空的ArrayList开始,它应该保存通过控制台添加的生日。我记下的这部分+它打印我添加的生日。但是如果我想添加另一个,它会打印我再次添加的第一个?我怎样才能让它打印出迄今为止我添加的所有生日?我将显示到目前为止的代码 生日班 public class Birthday { private String bdayKid; private int age; p
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;
}
//overridden toString() method
public String toString() {
return this.bdayKid + " turns " + this.age + "! They are" +
(this.gift ? "" : "not ") + " getting a gift.";
}
}
public class MainClass{
public static void main(String []args) {
ArrayList<Birthday> bdays = getBirthdays();
printBirthdays(bdays);
}
//This method will return a list of birthdays
public ArrayList<Birthday> getBirthdays() {
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
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);
}
}
}
public class MainClass{
public static void main(String []args) {
// Instantiate your one and only bdays array
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
getBirthdays(bdays);
printBirthdays(bdays);
}
//This method will return a list of birthdays
public void getBirthdays(ArrayList<Birthday> bdays) {
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
// Good practice to close a scanner
scan.close();
}
//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);
}
}
}
主类
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;
}
//overridden toString() method
public String toString() {
return this.bdayKid + " turns " + this.age + "! They are" +
(this.gift ? "" : "not ") + " getting a gift.";
}
}
public class MainClass{
public static void main(String []args) {
ArrayList<Birthday> bdays = getBirthdays();
printBirthdays(bdays);
}
//This method will return a list of birthdays
public ArrayList<Birthday> getBirthdays() {
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
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);
}
}
}
public class MainClass{
public static void main(String []args) {
// Instantiate your one and only bdays array
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
getBirthdays(bdays);
printBirthdays(bdays);
}
//This method will return a list of birthdays
public void getBirthdays(ArrayList<Birthday> bdays) {
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
// Good practice to close a scanner
scan.close();
}
//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);
}
}
}
是否需要在for each循环中添加if语句?感谢您的帮助 在
生日
类中添加字段:
private boolean printed = false;
然后在打印功能上:
//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) {
if(!bday.isPrinted())
{
System.out.println(bday);
bday.setPrinted(true);
}
}
}
//此方法将打印生日列表
公共无效打印生日(ArrayList B天){
//重复每个生日并打印结果
生日(B日:B日){
如果(!bday.isPrinted())
{
系统输出打印项次(b天);
b日期设置打印(真);
}
}
}
在生日
类中添加字段:
private boolean printed = false;
然后在打印功能上:
//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) {
if(!bday.isPrinted())
{
System.out.println(bday);
bday.setPrinted(true);
}
}
}
//此方法将打印生日列表
公共无效打印生日(ArrayList B天){
//重复每个生日并打印结果
生日(B日:B日){
如果(!bday.isPrinted())
{
系统输出打印项次(b天);
b日期设置打印(真);
}
}
}
您在理解Java方面遇到了严重的问题。首先,你需要理解一个概念,一个变量的范围
如果在函数中创建变量,则变量的作用域仅在函数中
您有标识符为bdays
的ArrayList
的不同实例。在void main
函数中定义的一个实例,以及每次调用getBirthdays()
方法时创建的其他实例。在switch语句中,当调用getBirthdays()
时,函数将形成并返回一个新的ArrayList
,而原始的ArrayList bdays
保持不变。当您调用printBirthdays(bdays)
时,它将参数作为您在主方法的第一行中创建的数组,而不是getBirthdays()
方法返回的新ArrayList
。因此,它不会打印新的Brithday
。因此,您可以按如下方式更改代码:
主类
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;
}
//overridden toString() method
public String toString() {
return this.bdayKid + " turns " + this.age + "! They are" +
(this.gift ? "" : "not ") + " getting a gift.";
}
}
public class MainClass{
public static void main(String []args) {
ArrayList<Birthday> bdays = getBirthdays();
printBirthdays(bdays);
}
//This method will return a list of birthdays
public ArrayList<Birthday> getBirthdays() {
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
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);
}
}
}
public class MainClass{
public static void main(String []args) {
// Instantiate your one and only bdays array
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
getBirthdays(bdays);
printBirthdays(bdays);
}
//This method will return a list of birthdays
public void getBirthdays(ArrayList<Birthday> bdays) {
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
// Good practice to close a scanner
scan.close();
}
//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);
}
}
}
请注意,Java将对象作为引用传递,因此如果您将
**bdays**
传递给getBirthdays(bdays)
,则此函数中对bdays
所做的每一项更改也将出现在main
方法中的原始bdays
中。更准确地说,它们在内存中是相同的ArrayList
,变量bdays
使用两种不同的方法main
和getBirthdays(bdays)
只是引用在函数开始时创建的相同ArrayList
并作为对getBirthdays
方法的引用传递的不同标识符。查看有关在java中通过引用传递变量的其他相关教程。您在理解java时遇到了严重问题。首先,你需要理解一个概念,一个变量的范围
如果在函数中创建变量,则变量的作用域仅在函数中
您有标识符为bdays
的ArrayList
的不同实例。在void main
函数中定义的一个实例,以及每次调用getBirthdays()
方法时创建的其他实例。在switch语句中,当调用getBirthdays()
时,函数将形成并返回一个新的ArrayList
,而原始的ArrayList bdays
保持不变。当您调用printBirthdays(bdays)
时,它将参数作为您在主方法的第一行中创建的数组,而不是getBirthdays()
方法返回的新ArrayList
。因此,它不会打印新的Brithday
。因此,您可以按如下方式更改代码:
主类
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;
}
//overridden toString() method
public String toString() {
return this.bdayKid + " turns " + this.age + "! They are" +
(this.gift ? "" : "not ") + " getting a gift.";
}
}
public class MainClass{
public static void main(String []args) {
ArrayList<Birthday> bdays = getBirthdays();
printBirthdays(bdays);
}
//This method will return a list of birthdays
public ArrayList<Birthday> getBirthdays() {
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
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);
}
}
}
public class MainClass{
public static void main(String []args) {
// Instantiate your one and only bdays array
ArrayList<Birthday> bdays = new ArrayList<Birthday>();
getBirthdays(bdays);
printBirthdays(bdays);
}
//This method will return a list of birthdays
public void getBirthdays(ArrayList<Birthday> bdays) {
Scanner scan = new Scanner(System.in);
//create a birthday
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
//add the birthday to arraylist of birthdays
bdays.add(bday);
// Good practice to close a scanner
scan.close();
}
//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);
}
}
}
请注意,Java将对象作为引用传递,因此如果您将
**bdays**
传递给getBirthdays(bdays)
,则此函数中对bdays
所做的每一项更改也将出现在main
方法中的原始bdays
中。更准确地说,它们在内存中是相同的ArrayList
,变量bdays
使用两种不同的方法main
和getBirthdays(bdays)
只是引用在函数开始时创建的相同ArrayList
并作为对getBirthdays
方法的引用传递的不同标识符。查看java中通过引用传递变量的其他相关教程。我认为最好的方法是将getBirthdays()拆分为printBirthdays()和AddBirthdays()。以下是代码,您可以使用自己的逻辑来循环输入:
import java.util.ArrayList;
import java.util.Scanner;
public class MainClass {
static ArrayList<Birthday> bdays = new ArrayList<>();
public static void main(String []args) {
while(true) {
//Use this method every time you need to add new BDay
addBirthday();
//Use this method to print all BDays in ArrayList
printBirthdays();
}
}
public static void addBirthday(){
Scanner scan = new Scanner(System.in);
Birthday bday = new Birthday(scan.nextLine(), scan.nextInt(), scan.nextBoolean());
bdays.add(bday);
}
public static void printBirthdays(){
for(Birthday b: bdays){
System.out.println(b);
}
}
}
import java.util.ArrayList;
导入java.util.Scanner;
公共类主类{
静态ArrayList bdays=新ArrayList();
公共静态void main(字符串[]args){
while(true){
//每次需要添加新的BDay时,请使用此方法
添加生日();
//使用此方法打印ArrayList中的所有B天
打印生日();
}
}
公共静态void addbirth(){
扫描仪扫描=新扫描仪(System.in);
生日bday=新生日(scan.nextLine(),scan.nextInt(),scan.nextBoolean());
b天。添加(b天);
}
公共静态vo