简体   繁体   English

Java如何在将一个对象添加到另一个ArrayList时将其删除?

[英]Java how to remove an object from one ArrayList while adding it to another?

This is my first time asking on this site, and got my exam tomorrow so trying to make my code great! 这是我第一次在此网站上提问,明天又通过了考试,因此请尝试使我的代码更好! I will link the whole class (Even though its 3 others). 我将链接整个班级(即使是其他3个班级)。

This is the method, and I cant manage to remove an object from lagerplass , adding it to hk.addVarer works tho. 这是方法,我无法设法从lagerplass删除对象,将其添加到hk.addVarer可以。 The idea behind all this is that lagerplass is were the items is stored in a store(butikk), so when people buy an item from the store and put it in handlekurv the items gets removed from the storage(lagerplass) : 所有这些背后的想法是, lagerplass是将物品存储在商店中(butikk),因此,当人们从商店购买物品并将其放入handlekurv ,物品会从仓库中移除(lagerplass):

public void leggTilHandlekurv(String varenavn)
    {
        for (Varer a : lagerplass)
        {
            if (a.getVarenavn().equals(varenavn))
            {
                hk.addVarer(a);
                lagerplass.remove(varenavn);
            }
        }
    }

Here is the kode for the whole Butikk class 这是整个Butikk课的主题

public class Butikk
{
// instance variables - replace the example below with your own
public ArrayList<Varer> lagerplass;
Handlekurv hk = new Handlekurv();

/**
 * Constructor for objects of class Butikk
 */
public Butikk()
{
    // initialise instance variables
    lagerplass = new ArrayList<Varer>();
}

/**
 * An example of a method - replace this comment with your own
 * 
 * @param  y   a sample parameter for a method
 * @return     the sum of x and y 
 */
public void nyHeadset(String lyd, String vare,int pris, String varenavn )
{
    // put your code here
    Headset nyHeadset = new Headset ( lyd, vare, pris, varenavn);
    lagerplass.add(nyHeadset);

}

public void nyMus(String vare, int pris, String varenavn, int dpi)
{
    Mus nyMus = new Mus(vare, pris, varenavn, dpi);
    lagerplass.add(nyMus);
}

public void printLagerplass()
{
    for (Varer vare : lagerplass)
    {
        vare.printDetails();
    }
}

public int lagerplassSize()
{
    return lagerplass.size();
}

public void fjernHeleLagerplass()
{
    lagerplass.clear();
}

public void leggTilHandlekurv(String varenavn) 
{ 
Iterator<Varer> iterator = lagerplass.iterator();
while(iterator.hasNext()) {
     Varer a = iterator.next();
     if (a.getVarenavn().equals(varenavn)) {
         iterator.remove();
         hk.addVarer(a);
     }
}

} }

public void printHeleHandlekurven()
{
    hk.printHandlekurv();
}

}enter code here }在这里输入代码

Here is the kode for the whole Varer class 这是整个Varer课程的主题

enter code here public class Varer { // Representerer merke og pris til en vare. 在此处输入代码public class Varer {//代表代理人。 private String vare; 私有字符串变量; private String merke; 私有String merke; private int pris; 私人诠释 private String varenavn; 私有字符串varenavn;

/**
 * Constructor for klassen Varer
 */
public Varer(int pris, String varenavn, String vare)
{
    merke = "Razor";
    this.pris = pris;
    this.varenavn = varenavn;
    this.vare = vare;

}

public String getMerke()
{
    return merke;
}

public int getPris() 
{
    return pris;
}

public String getVarenavn()
{
    return varenavn;
}

public String getVare()
{
    return vare;
}

public String getDetails()
{
    return vare + ", " + merke +" " + varenavn + ", "+pris + " kr.";
}
public void printDetails()
{
    System.out.println(vare + ", " + merke +" "+ varenavn + ", "+pris + " kr.");
    System.out.println();
}

Removing an item from a collection while iterating over it should yield a ConcurrentModificationException , which I presume is what you are getting and is why you cannot remove from it. 从一个集合中删除一个项目,同时对其进行迭代,应该会产生ConcurrentModificationException ,我想这就是您所得到的,这就是为什么您不能从其中删除它。

To remove an element while iterating over a collection, you will need to get that collection's iterator and remove it through the collection's iterator. 要在迭代集合时删除元素,您将需要获取该集合的迭代器,并通过集合的迭代器将其删除。

public void leggTilHandlekurv(String varenavn) { 
    Iterator<Varer> iterator = lagerplass.iterator();
    while(iterator.hasNext()) {
         Varer a = iterator.next();
         if (a.getVarenavn().equals(varenavn)) {
             iterator.remove();
             hk.addVarer(a);
         }
    }
}

Iterator.remove is the only safe way to modify a collection during iteration; Iterator.remove是在迭代过程中修改集合的唯一安全方法。 the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress. 如果在进行迭代时以任何其他方式修改了基础集合,则行为未指定。

for (Iterator<Varer> iterator = lagerplass.iterator(); iterator.hasNext();) {
    Varer a = iterator.next();
    if (a.getVarenavn().equals(varenavn))
    {
        hk.addVarer(a);
        // Remove the current element from the iterator and the list.
        iterator.remove();
    }
}

Source: 资源:

http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html

It is because you cannot modify a collection while iterating over it. 这是因为您无法在迭代集合时修改集合。 Either use a for loop with indeces, or an Iterator over your collection and call the remove method. 使用带indees的for循环,或在集合上使用Iterator并调用remove方法。

暂无
暂无

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

相关问题 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? Java,如何将一个对象添加到ArrayList并从另一个对象减少一个对象 - Java, how to add one object to an ArrayList and decrease one from another 如何从 Java 中的 ArrayList 中删除特定对象? - How to remove specific object from ArrayList in Java? 如何使用Java将数据从一个ArrayList添加到另一个ArrayList? - How to add data from one ArrayList to another ArrayList using Java? 如何在Java中使用扫描仪时删除ArrayList中的特定对象 - how to remove specific object in an ArrayList while using a scanner in java 将一个类的对象添加到另一类的数组列表中 - Adding object of one class to the arraylist in another class 如何使用 Java ArrayList 删除 object 的一个实例 - How to remove one instance of an object using Java ArrayList 如何根据对象的条件从java中的数组列表返回(和删除)对象 - How to return (and remove) an object from an arraylist in java based on a condition of that object 如果第二个数组中的值与第一个数组中的值相同,如何从另一个数组中删除一个值 - How to remove one arraylist value from another arraylist if the value in second array is same in first arraylist 如何在迭代时从arraylist中删除一个元素 - How to remove just one element from arraylist while iterating over it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM