Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从一个ArrayList提取并修改到另一个ArrayList_Java_Arraylist - Fatal编程技术网

Java 从一个ArrayList提取并修改到另一个ArrayList

Java 从一个ArrayList提取并修改到另一个ArrayList,java,arraylist,Java,Arraylist,我需要一个ArrayList选择唯一元素并添加到另一个ArrayList。LineItem包含硬币和第一个列表中匹配此硬币的数量 我的代码可以工作,但不正确,我不知道如何绕过这个限制 以下是检查输出: // it's list of coin's element LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]] LineItem [quantity=1, theCoin=Coin[value=0.25,name=q

我需要一个
ArrayList
选择唯一元素并添加到另一个
ArrayList
。LineItem包含硬币和第一个列表中匹配此硬币的数量

我的代码可以工作,但不正确,我不知道如何绕过这个限制

以下是检查输出:

// it's list of coin's element
LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]  
LineItem [quantity=1, theCoin=Coin[value=0.25,name=quantity dollar]] 
LineItem [quantity=1, theCoin=Coin[value=0.25,name=quantity dollar]]
LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]
LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]

// list of LineItem's element
[  
    LineItem [quantity=2, theCoin=Coin[value=0.5,name=half dollar]],   
    LineItem [quantity=2, theCoin=Coin[value=0.25,name=quantity dollar]],   
    LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]  
]
LineItem
元素的列表应仅包含具有唯一
名称的元素。
若名称进入列表,则只应更新此元素的数量。
(我们在硬币半美元和数量美元中只有两个元素。行项目列表必须只有这两个硬币名称,并与硬币列表中的数量相匹配)

代码:

private static ArrayList<LineItem> createItems(ArrayList<Coin> coins) {
    ArrayList<LineItem> itemsList = new ArrayList<LineItem>();

    for (Coin aCoin : coins) {
        LineItem anItem = new LineItem(aCoin, 1);
        System.out.println(anItem.toString()); // print every Coin element
        if (!itemsList.contains(anItem)) {
            itemsList.add(anItem);
        } else {
            int i = 0;
            boolean done = false;
            while (!done & i <= itemsList.size()) {
                Coin currentCoin = itemsList.get(i).getCoin();
                int currentQuantity = itemsList.get(i).getQuantity();
                if (currentCoin.equals(aCoin)) {
                    itemsList.get(i).setQuantity(currentQuantity + 1);
                    done = true;
                } else {
                    i++;
                }
            }
        }
    }
    System.out.println("\n" + itemsList.toString()); // print final LineItem's list
    return itemsList;
}
私有静态ArrayList createItems(ArrayList硬币){
ArrayList itemsList=新的ArrayList();
用于(硬币:硬币){
LineItem anItem=新的LineItem(aCoin,1);
System.out.println(anItem.toString());//打印每个硬币元素
如果(!itemsList.contains(anItem)){
项目列表添加(项目);
}否则{
int i=0;
布尔完成=假;

虽然(!done&i我建议另一种方法来实现这一目标:

  • 重写Coin类中的equals方法,使其返回 根据硬币价值判断正确/错误

  • 迭代数组,在地图中添加每个具有特定值的硬币,键作为硬币面额,值作为数量(
    如[0.25,1];[0.5,2]

  • 继续汇总您的数量,并替换此地图中特定面额的值;这样,在地图的末尾看起来:

  • 0.25,3-3数量的0.25面额硬币

    0.5、4-4数量的0.5面额硬币


    4最后遍历映射并创建要添加到ArrayList的LineItem对象。

    尝试重写LineItem类中的
    equals()

    @Override
    public boolean equals (Object obj) {
       return this.getCoin.equals (((LineItem)obj).getCoin);
    }
    

    这应该允许列表上的
    contains()
    方法检查硬币是否已经在列表中,而不管相关数量如何。

    我认为问题在于
    等于
    行项目的
    -不仅要检查
    硬币,还要检查数量

    因此,当您调用
    List.contains
    时,如果您处理了两个相同的硬币,它将返回
    false

    代码的固定版本可能如下所示

    for (Coin aCoin : coins) {
        boolean done = false;
        for (Iterator<LineItem> it = itemList.iterator();it.hasNext() && !done) {
            LineItem currentItem = it.next()
            Coin currentCoin = currentItem.getCoin();
            int currentQuantity = currentItem.getQuantity();
            if (currentCoin.equals(aCoin)) {
                currentItem.setQuantity(currentQuantity + 1);
                done = true;
            }
        }
        if(!done) {
            LineItem anItem = new LineItem(aCoin, 1);
            itemsList.add(anItem);
        }
    }
    
    for(硬币币:硬币){
    布尔完成=假;
    for(Iterator it=itemList.Iterator();it.hasNext()&&!done){
    LineItem currentItem=it.next()
    Coin currentCoin=currentItem.getCoin();
    int currentQuantity=currentItem.getQuantity();
    if(当前硬币等于(aCoin)){
    currentItem.setQuantity(当前数量+1);
    完成=正确;
    }
    }
    如果(!完成){
    LineItem anItem=新的LineItem(aCoin,1);
    项目列表添加(项目);
    }
    }
    
    但我更喜欢在这种情况下使用地图

    添加地图代码

    Map<Coin, LineItem> map = new HashMap<Coin, LineItem>();
    for (Coin aCoin : coins) {
        LineItem anItem = map.get(aCoin);
        if(anItem == null) {
            map.put(aCoin, new LineItem(aCoin, 1));
        } else {
            anItem.setQuantity(anItem.getQuantity()+1);
        }
    }
    itemsList.addAll(map.values());
    
    Map Map=newhashmap();
    用于(硬币:硬币){
    LineItem anItem=map.get(aCoin);
    if(anItem==null){
    地图输入(aCoin,新行项目(aCoin,1));
    }否则{
    anItem.setQuantity(anItem.getQuantity()+1);
    }
    }
    itemsList.addAll(map.values());
    

    但是,如果你不使用
    Coin#hashCode

    的话,这就行不通了。如果我已经理解了你想要实现的目标,那就是

    您有第一个列表,其中包含:-

    LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]  
    LineItem [quantity=1, theCoin=Coin[value=0.25,name=quantity dollar]] 
    LineItem [quantity=1, theCoin=Coin[value=0.25,name=quantity dollar]]
    LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]
    LineItem [quantity=1, theCoin=Coin[value=0.5,name=half dollar]]
    
    但看起来你希望结果是:-

    [LineItem [quantity=3, theCoin=Coin[value=0.5,name=half dollar]], LineItem [quantity=2, theCoin=Coin[value=0.25,name=quantity dollar]]
    
    如果我是你来解决这个问题,我会在硬币上实现equals方法,它会比较值和名称。虽然在你的例子中,值看起来足够了

    例如

    public boolean equals(Coin coinToCompare) {
        return value.equals(coinToCompare.getValue()) && name.equals(coinToCompare.getName());
    }
    
    如果您只是执行equals()而没有跳过equals()方法,那么它将只执行引用比较,而不是按照您希望的方式进行比较


    此外,我认为您最好使用一个以键作为硬币值的HashMap,而不是使用允许重复的列表。

    我不明白问题出在哪里。
    中(!done&i@tieTYT LineItem元素的列表包含具有相同硬币名称和不同数量的元素。该元素的数量应仅递增。在
    LineItem
    类的
    equals
    方法中,可能考虑了数量。当数量为1时,会找到该数量,并且数量被更新为2。然后它不再匹配,并创建一个单独的
    行项目
    。请在
    行项目
    @Diego C Nascimento上发布你的
    equals
    方法。如果我们没有在所有列表中找到元素会怎么样?使用
    和&
    -done,保持为false,我们在
    期间从不检查
    的第二部分