简体   繁体   English

为什么我的ArrayList打印第一个对象3次?

[英]Why does my ArrayList print the first object 3 times?

I am relatively new to Java. 我对Java比较陌生。 I was practicing an exercise where we explore the polymorphic behavior of subclass and superclass instances. 我在练习中探索子类和超类实例的多态行为。 This exercise builds on the concepts of abstract classes and interfaces. 本练习基于抽象类和接口的概念。 ArrayLists are introduced in this exercise. 在本练习中介绍了ArrayList。 One of the questions requires us to create an ArrayList of WrittenWork objects called products, and add the appropriate objects to the ArrayList. 问题之一就是要求我们创建一个称为产品的WrittenWork对象的ArrayList,并将适当的对象添加到ArrayList中。

My problem is that the ArrayList keeps printing the first item 3 times and then prints the rest of the items. 我的问题是ArrayList继续打印第一个项目3次,然后打印其余项目。

Could someone explain why this keeps happening? 有人可以解释为什么这种情况持续发生吗?

EDIT: I realized the mistake I was making thanks to you guys! 编辑:我意识到我要感谢你们的错误! I changed the for loop from 我将for循环从

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

To

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

This Image shows how all the classes are connected 此图显示了所有类如何连接

Here is the code: 这是代码:

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 这是输出

Other than not having used brackets to correctly identify the for block, you have a logic flaw in your small piece of code. 除了没有使用方括号正确识别for块外,您的一小段代码还存在逻辑缺陷。

You wrote 你写了

the ArrayList keeps printing the first item 3 times and then prints the rest of the items ArrayList继续打印第一个项目3次,然后打印其余项目

This lead me to think your code doesn't really represents what you actually want to accomplish. 这使我认为您的代码并不能真正代表您实际想要完成的工作。
This is totally normal in the beginning, don't worry. 一开始这是完全正常的,请放心。 It seems you'd like to print all the WrittenWork . 看来您想打印所有的WrittenWork

For that you used an ( enhanced ) for loop 为此,您使用了( 增强的for循环

for (WrittenWork w : products)

but you never taken into consideration the w local variable. 但您从未考虑过w局部变量。

What happens in the background is that an Iterator<WrittenWork> ( JavaDoc ) is created for you, and each iteration the next element in the products list is given to you via w . 后台发生的事情是为您创建了Iterator<WrittenWork>JavaDoc ),并且每次迭代都会通过wproducts列表中的下一个元素提供给您。

You just need to use that w variable to print all the elements of products . 您只需要使用w变量来打印products所有元素。
The Iterator will exhaust itself and the loop will stop. Iterator将耗尽自身,循环将停止。


For your knowledge, this for-loop style 就您所知,这种for循环样式

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

is equivalent to 相当于

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

You can see the checked condition is iterator.hasNext(); 您可以看到检查的条件是iterator.hasNext(); .

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

   }

In a program exection fashion it may make you feel right and it runs but the logical issue incurs that if you opt for printing all objects with segregate System.out.println methods, then why did you use for(each)-loop ? 以程序执行的方式,它可能会让您感觉正确并可以运行,但是逻辑上的问题是,如果您选择使用隔离的System.out.println方法打印所有对象,那么为什么要使用for(each)-loop

Besides, you don't need to call toString() method called implicitly and automatically if an object reference is passed to System.out.println method 此外,如果将对象引用传递给System.out.println方法,则无需调用隐式且自动调用的toString()方法。

It should be: 它应该是:

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

  }

You're already going over all the WrittenWork objects, using w1, w2, w3 nullifies the use of using for-loop in the first place. 您已经遍历了所有WrittenWork对象,使用w1,w2,w3首先使使用for循环无效。

You are iterating over the ArrayList but printing out w1 , w2 and w3 on every iteration. 您正在遍历ArrayList,但在每次迭代中都打印出w1w2w3 As others pointed out print w in every iteration which points to the element corresponding to the iteration. 正如其他人指出的那样,每次迭代中的print w都指向与迭代相对应的元素。

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

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM