为什么我的ArrayList会频繁地打印元素,就像在调用它时一样

huangapple go评论93阅读模式
英文:

Why is my ArrayList prints the elements as frequent as when it's called

问题

以下是已经翻译好的内容:

我正在使用Java为学用品商店做一个项目。我已经使用构造函数声明了产品,并将这些元素存储在一个ArrayList中。我有多个方法可以添加、减少数量,修改单价,添加和显示所有产品。每次我想要显示产品时,它都会像在程序中调用了多少次一样显示列表。例如,首先我调用显示所有产品的方法,它会显示一次。第二次我调用显示所有产品的方法,它将会将所有产品显示两次,依此类推,就像这样:

这是我的显示所有产品的方法:

public static void displayProducts() {
    inventoryList.add(product);
    inventoryList.add(oslo);
    inventoryList.add(pilot);
    inventoryList.add(mongol);
    inventoryList.add(hbw);
    inventoryList.add(crayola);
    inventoryList.add(fabercastell);
    inventoryList.add(orion);
    inventoryList.add(easywrite);
    
    for (int i = 1; i < inventoryList.size(); i++) 
        inventoryList.get(i).outputDisplay();
}
    
public void outputDisplay() {
    System.out.println(String.format("|%-21s\t%-18s\t%5.2f\t%8d|", brand, type, price, quantity));
}
英文:

I'm doing a project for school supplies store using Java. I've declared the products using a constructor and I've stored the elements in an ArrayList. I have multiple methods which can add, subtract quantities, modify unit price, add and display all products. Every time I want to display the products it displays the list just like how many times it was called in the program. For example, first I call my display all product method, it will display one time. The second I call my display all product method, it will display all the products twice and so on just like this:

为什么我的ArrayList会频繁地打印元素,就像在调用它时一样

This is my display all product method:

public static void displayProducts() {
	
	inventoryList.add(product);
	inventoryList.add(oslo);
	inventoryList.add(pilot);
	inventoryList.add(mongol);
	inventoryList.add(hbw);
	inventoryList.add(crayola);
	inventoryList.add(fabercastell);
	inventoryList.add(orion);
	inventoryList.add(easywrite);
	
	for (int i = 1; i &lt; inventoryList.size(); i++) 
		inventoryList.get(i).outputDisplay();
			
}

public void outputDisplay() {
    System.out.println(String.format(&quot;|%-21s\t%-18s\t%5.2f\t%8d|&quot;, brand, type, price, quantity));
}

答案1

得分: 1

你的 displayProducts() 方法不仅仅是用来展示产品的。它首先会将产品添加到库存中,然后再进行展示。所以第一次调用该方法时,它会先添加这些元素,然后再展示它们。第二次调用时,它会再次添加这些元素,但由于它们已经存在了,看起来就好像是将它们展示了两次。但实际上,它只是将列表中的内容展示了一次。问题在于,你应该独立于展示方法,单独将这些元素添加到库存中,但现在却没有这样做。

英文:

Your displayProducts() method does more than displaying the products. It adds them to the inventory first, then displays them. So the first time you call it, it adds the elements and displays them. The second time you call it, it adds the elements, and since they were already there, it's like it's displaying them twice. But really, what it's doing is displaying what's in the list once. The problem is that you should be adding the elements to the inventory independently of the method that shows them, but you aren't.

答案2

得分: 1

你应该将这个方法分成两个方法,一个用于添加元素,另一个用于显示元素,因为每次调用你现在的方法时,你既添加又显示了这些元素。

英文:

You should split the method in 2 methods, one that adds the elements and one that displays them, because every time you call your current method you both add and display the elements.

答案3

得分: 1

Your displayProducts method not only displays the inventory, but it also adds products to the inventory.

The displayProducts method should only display products, otherwise you'll get buggy and confusing behavior.

public static void displayProducts() {
    for (int i = 1; i < inventoryList.size(); i++) 
        inventoryList.get(i).outputDisplay();            
}

You'll want to have a separate method that adds the products to the inventory. You would call this method only once.

public static void addProducts() {
    inventoryList.add(product);
    inventoryList.add(oslo);
    inventoryList.add(pilot);
    inventoryList.add(mongol);
    inventoryList.add(hbw);
    inventoryList.add(crayola);
    inventoryList.add(fabercastell);
    inventoryList.add(orion);
    inventoryList.add(easywrite);
}
英文:

Your displayProducts method not only displays the inventory, but it also adds products to the inventory.

The displayProducts method should only display products, otherwise you'll get buggy and confusing behavior.

public static void displayProducts() {
    for (int i = 1; i &lt; inventoryList.size(); i++) 
        inventoryList.get(i).outputDisplay();            
}

You'll want to have a separate method that adds the products to the inventory. You would call this method only once.

public static void addProducts() {
    inventoryList.add(product);
    inventoryList.add(oslo);
    inventoryList.add(pilot);
    inventoryList.add(mongol);
    inventoryList.add(hbw);
    inventoryList.add(crayola);
    inventoryList.add(fabercastell);
    inventoryList.add(orion);
    inventoryList.add(easywrite);
}

huangapple
  • 本文由 发表于 2020年8月28日 21:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63634929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定