0

As u can see i wanna delete or remove the ingrediant which equals my ingredientid from my SplitedIgrediants list, i've tried with remove or delete but it's appear an error. So how can i do to delete this ingredient elemant please from my list in JAVA.

        String ingredientid = request.getParameter("id");
        DbHandler dbsplt = new DbHandler();
        for (String ingrediant : SplitedIngrediants) {
            if (ingrediant.equals(ingredientid)) {
                //HERE REMOVE THE ingredient from SplitedIngrediants list
            }
9
  • 1
    how are we to know? we don't even know the type of SplitedIngrediants Commented Apr 5, 2018 at 9:06
  • What is SplitedIngrediants. Best we see the other parts of the code Commented Apr 5, 2018 at 9:08
  • 2
    also: why do you tag jquery for a java question? Commented Apr 5, 2018 at 9:08
  • @Stultuske look at the code closely, it's a for each loop, so since it's wrote in the title and the sub element is of type String, i guess SplitedIngrediants is a List of type String aswell. Commented Apr 5, 2018 at 9:09
  • 3
    @xoxel you have no idea how many people think String[] myList is a 'list of Strings' Commented Apr 5, 2018 at 9:10

2 Answers 2

1

You cannot remove elements from a Collection while iterating on it (you will get some ConcurrentModificationException) except if you manually declare the iterator and use iterator.remove().

Example :

List<Integer> list = new ArrayList<Integer>();

list.add(3);
list.add(4);
list.add(5);

Iterator<Integer> it = list.iterator();
Integer current;
while (it.hasNext()) {
    current = it.next();
    if(current.equals(4)) {
        it.remove();
    }
}

Ouput :

[3, 5]

The reason behind that is that the "foreach" construction internally creates an iterator. The aim of the iterator is to ensure that each element of the iterable is visited exactly once. So if you add/remove elements from the iterable without using the iterator methods, the iterator can no longer fulfil its task.

2nd option : while iterating on the list, make a list of the items to delete and delete them after iterating.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

String ingredientid = request.getParameter("id");
SplitedIngrediants.removeAll(Collections.singleton(ingredientid));

Example code:

public static void main(String args[]) {
        List<String> l = new ArrayList<String>();
        l.add("first");
        l.add("first");
        l.add("second");

        String ingredientid = "first";

        l.removeAll(Collections.singleton(ingredientid));

        System.out.println(l);
    }

With SplitedIngrediants is a String array try this:

public static void main(String args[]) {
    String[] SplitedIngrediants = new String[] { "first", "first", "second" };

    String ingredientid = "first";

    List<String> NewSplitedIngrediants = new ArrayList<>();
    for (String ingrediant : SplitedIngrediants) {
        if (!ingrediant.equals(ingredientid)) {
            NewSplitedIngrediants.add(ingrediant);
        }
    }
    SplitedIngrediants = new String[NewSplitedIngrediants.size()];
    SplitedIngrediants = NewSplitedIngrediants.toArray(SplitedIngrediants);
    for (String ingrediant : SplitedIngrediants) {
        System.out.println(ingrediant);
    }

}

7 Comments

What Collections for and singleton for ?
It's said that cannot find symobol of removeAll
SplitedIngrediants is a java.util.List? If yes then there must be removeAll() method.
No i declare like this String[] SplitedIngrediants a table of strings
Because what i do actually is that i split a string with "," and add it to my string table
|

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.