【问题标题】:Why does my ArrayList print the first object 3 times?为什么我的 ArrayList 打印第一个对象 3 次?
【发布时间】:2019-08-07 11:47:16
【问题描述】:

我对 Java 比较陌生。我正在练习一个练习,我们探索子类和超类实例的多态行为。本练习建立在抽象类和接口的概念之上。 本练习中介绍了 ArrayList。 其中一个问题要求我们创建一个名为 products 的 WrittenWork 对象的 ArrayList,并将适当的对象添加到 ArrayList。

我的问题是 ArrayList 一直打印第一个项目 3 次,然后打印其余项目。

有人能解释一下为什么会这样吗?

编辑:感谢你们,我意识到我犯的错误! 我从

更改了 for 循环
   for(WrittenWork w: products)
       System.out.println(w1.toString());
       System.out.println(w2.toString());
       System.out.println(w3.toString());

for(WrittenWork w: products)
    System.out.println(w.toString());

This Image shows how all the classes are connected

代码如下:

import java.util.*;

public class BookStore
{
  public static void main(String[] args)
  {
   Author a1 = new Author("Malcom Gladwell");
   Author a2 = new Author("Steven Johnson");
   Author a3 = new Author("Mathias Johansson");
   Author a4 = new Author("Evan Ackerman");
   Author a5 = new Author("Erico Guizzo");
   Author a6 = new Author("Fan Shi");

   WrittenWork w1 = new Novel(a1, "What the Dog Saw and other adventures", 503);
   WrittenWork w2 = new Novel(a2, "How We Got to Now: Six Innovations That Made the Modern World", 320);
   WrittenWork w3 = new Novel(a2, "Everything Bad Is Good For you: How Today's Popular Culture is Actually Making us Smarter", 254);


   ArrayList<WrittenWork>products = new ArrayList<>();
   products.add(w1);
   products.add(w2);
   products.add(w3);

   for(WrittenWork w: products)
       System.out.println(w1.toString());
       System.out.println(w2.toString());
       System.out.println(w3.toString());

   }
 }

This is the output

【问题讨论】:

  • 你的循环没有括号,所以只执行第一条语句。如果您对打印语句进行硬编码,为什么还要使用循环?
  • 为什么不打印 w?

标签: java arraylist bluej


【解决方案1】:

除了没有使用方括号正确识别for 块之外,您的一小段代码存在逻辑缺陷。

你写的

ArrayList 保持打印第一项 3 次,然后打印 其余物品

这让我觉得你的代码并不能真正代表你真正想要完成的事情。
一开始这很正常,不用担心。 您似乎想打印所有WrittenWork

为此,您使用了(增强for 循环

for (WrittenWork w : products)

但您从未考虑过 w 局部变量。

在后台发生的事情是为您创建了一个Iterator&lt;WrittenWork&gt; (JavaDoc),并且每次迭代products 列表中的 next 元素通过@987654329 提供给您@。

您只需要使用该w 变量来打印products 的所有元素。
Iterator 将自行耗尽并且循环将停止。


据您所知,这种 for 循环样式

for (WrittenWork w : products) { ... }

等价于

for (Iterator<WrittenWork> iterator = products.iterator(); iterator.hasNext(); ) {
    final WrittenWork w = iterator.next();
    ...
}

可以看到勾选的条件是iterator.hasNext();

【讨论】:

    【解决方案2】:
    for(WrittenWork w: products)
           System.out.println(w1.toString());
           System.out.println(w2.toString());
           System.out.println(w3.toString());
    
       }
    

    在程序执行方式中,它可能会让您感觉正确并且它可以运行,但逻辑问题是,如果您选择使用隔离的System.out.println 方法打印所有对象,那么您为什么要使用for(each)-loop

    此外,如果将对象引用传递给System.out.println 方法,则无需调用隐式自动调用的toString() 方法

    【讨论】:

      【解决方案3】:

      应该是:

        for(WrittenWork w: products)
             System.out.println(w.toString());
      
        }
      

      你已经遍历了所有的 WrittenWork 对象,首先使用 w1、w2、w3 使使用 for 循环无效。

      【讨论】:

        【解决方案4】:

        您正在迭代 ArrayList,但在每次迭代时打印出 w1w2w3。正如其他人指出的那样,在每次迭代中打印w,它指向与迭代对应的元素。

        for (WrittenWork w: products){
           System.out.println(w.toString()); 
        }
        

        【讨论】:

          【解决方案5】:
          for (WrittenWork w: products){
             System.out.println(w.toString()); 
          }
          

          【讨论】:

            【解决方案6】:

            试试这个:

            for (WrittenWork w: products){
               System.out.println(w.toString()); 
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2020-03-28
              • 1970-01-01
              • 2016-02-01
              • 1970-01-01
              • 2017-06-21
              • 1970-01-01
              • 1970-01-01
              • 2022-01-13
              相关资源
              最近更新 更多