简体   繁体   中英

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).

This is the method, and I cant manage to remove an object from lagerplass , adding it to hk.addVarer works tho. 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) :

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

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

enter code here public class Varer { // Representerer merke og pris til en vare. private String vare; private String merke; private int pris; private String 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.

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; 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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