2

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();
}
3
  • Check out this answer that addresses one aspect of your issue: stackoverflow.com/questions/223918/…. You will also need to be concerned about the fact that you are attempting to remove a String from a collection of Varer. Commented May 20, 2015 at 14:24
  • For a full answer, please provide the declarations of the two Collections and the Varer class. Commented May 20, 2015 at 14:31
  • FYI, removing from an ArrayList (unless you remove the last item) never ever looks great. (Using any other language than English is not really a great looking thing either.) Commented May 20, 2015 at 15:10

3 Answers 3

3

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);
         }
    }
}
Sign up to request clarification or add additional context in comments.

15 Comments

I gave this an up vote for the ConcurrentModificationException, but he may also have issues with attempting to remove a String from a collection of Varer, depending on whether/how he overrode .equals() in Varer.
@pens-fan-69: According to the Javadoc, the remove removes the last element returned by the iterator. It does not seem to be using equals. If that is not what you mean, please let me know.
I tried this and it removes, but it wont let me use my print method, then I get an error: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.remove(ArrayList.java:474) at Butikk.leggTilHandlekurv(Butikk.java:69)
@KennethRønning: I do not know what your print method does, so I can only speculate, but it would seem that you are using .get(1) on some ArrayList which is of size 1 (ArrayLists are 0 based in Java).
public void printLagerplass() { for (Varer vare : lagerplass) { vare.printDetails(); } }
|
0

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.

Comments

0

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

1 Comment

He doesn't iterate over strings but over Varer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.