【问题标题】:ArrayList<Item> (where Item is inner class) adding wrongArrayList<Item> (其中 Item 是内部类)添加错误
【发布时间】:2014-09-27 15:17:36
【问题描述】:

我有一个名为 Bag2 的类,它有一个名为 Item 的内部类。 Bag2 具有变量 ArrayList aList 和名为“add”的函数。重复添加重复值是添加错误。

这是我的代码:

import java.util.ArrayList;
public class Bag2 {

public Bag2(){}; // Constructor

/**
 * Inner class
 *
 */
public class Item implements Comparable<Item> {

    String name;
    int quantity;

    public Item(String name, int quantity) { // Constructor
        this.name = name;
        this.quantity = quantity;
    }

    @Override
    public String toString() {
        return name + " : " + quantity;
    }


    @Override
    public int compareTo(Item o) {
        return name.compareToIgnoreCase(o.name);
    }

}

public ArrayList<Item> aList = new ArrayList<>();

public void add(String itemName){

    Bag2 bag2 = new Bag2();
    Bag2.Item item = bag2.new Item(itemName.toUpperCase(), 1);

    if (aList.isEmpty()){
        aList.add(item);
    } else 
    {
        for(int i = 0; i < aList.size();i++){   
            if (item.compareTo(aList.get(i))==0){
                aList.get(i).quantity++;
            }else {
                aList.add(item); // Built inn add-function 
                break; // add one time only and the size increases
            }
        }
    }

}


}

这是我的测试:

public class Bag2Test {

public static void main(String[] args) {
    Bag2 bag = new Bag2();

    Bag2.Item[] anArray =  
        {
        bag.new Item("A", 1),
        bag.new Item("B", 1),
        bag.new Item("C", 1),
        bag.new Item("D", 1),
        bag.new Item("a", 1),
        bag.new Item("F", 1),
        bag.new Item("b", 1),
        bag.new Item("e", 1),
        bag.new Item("a", 1)

        };

    for (int i = 0; i<anArray.length; i++ ){
        bag.add(anArray[i].name); // 
    }

    System.out.println("\nA list contains : ");
    for (int i = 0; i<bag.aList.size(); i++) {
        System.out.println(bag.aList.get(i));
    }

}
}

和输出:

一个列表包含: 答:3 乙:1 C : 1 D : 1 答:1 F : 1 乙:1 : 1 答:1

【问题讨论】:

  • Item 类应该是静态的。它不使用其外部对象的任何方法字段。也就是说,你应该问一个问题。
  • 您的内部班级使用令人不安。让Item 成为static 类怎么样?我强烈建议使用调试器并逐步完成您的add 方法。您很快就会知道发生了什么。

标签: java class arraylist


【解决方案1】:

您的 add 函数已损坏,因为它可以为一个 i 值触发语句 if (item.compareTo(aList.get(i))==0) 并且仍然为另一个值添加它。虽然为您的程序提供了更优雅、更强大的解决方案,包括覆盖 equals()hashCode() 并使用 Set 而不是列表,但这将导致通用包实现,我发布了针对您的问题的最短解决方案。

public void add(String itemName)
{
    Bag2 bag2 = new Bag2();
    Bag2.Item item = bag2.new Item(itemName.toUpperCase(), 1);

    if (aList.isEmpty())
    {
        aList.add(item);
    } else 
    {
        boolean existing = false;
        for(int i = 0; i < aList.size();i++)
        {   
            if (item.compareTo(aList.get(i))==0)
            {
                aList.get(i).quantity++;
                existing=true;
                break;
            }               
        }
        if(!existing) {aList.add(item);}
    }
}

【讨论】:

    【解决方案2】:

    假设您添加了以下项目:A,B,C

    现在您的列表是:A:1, B:1, C:1

    在您的添加逻辑中,您检查当前项目是否相同,否则您添加该项目。因此,如果我们现在尝试再次添加项目 C,您的列表将如下所示:A:1, B:1, C:1, C:1

    这是因为您正在逐项检查。在添加新项目之前,您需要检查它是否不存在于整个列表中,然后才添加它。 (例如,将 C 添加到上述列表时,第一次循环迭代 (i=0) 将执行 else 块中的代码,因为 CA 是不同的,并且 C 将被添加,尽管它确实存在于列表中)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多