0

I have an Arraylist with (String-)Arrays in it. Now I want to be able to delete a specific element from the Arraylist.

Here is an example arraylist:

0:  [Name, , , ]
1:  [Telefon, \(\d+\) \d+, DAFAE8, FF6262]
2:  [E-Mail, ^[a-zA-Z0-9]+(?:(\.|_)[A-Za-z0-9!#$%&'*+/=?^`{|}~-]+)*@(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$, DAFAE8, FF6262]
3:  [Company, , , ]
4:  [Test, , , ]
5:  [Test2, , , ]
6:  [Test3, , , ]

Now I want to delete the 5th element (the array which includes Test2). How do I accomplish that?

I already tried the following things:

public static List<String[]> removeSpecificElements(List<String[]> list, int i){
    list.remove(new Integer(i));
    return list;
}

This one doesn't throw any kind of Exception, but doesn't work either.

public static List<String[]> removeSpecificElements(List<String[]> list, int i){
    list.remove(i);
    return list;
}

This one throws ArrayIndexOutOfBounds.

public static List<String[]> removeSpecificElements(List<String[]> list, int i){
    Iterator<String[]> itr = list.iterator();
    itr.next();
    for (int x = 0; x < i; x++){
        itr.next();
        System.out.println(itr);
    }
    itr.remove();
    return list;
}

And this one always removes the first element.

Could you please help me?

1
  • There are numerous questions about how to delete elements from a list on this site. Did you bother searching for them first? Commented Feb 25, 2016 at 9:28

2 Answers 2

4

Now I want to delete the 5th element (the array which includes Test2). How do I accomplish that?

You need to use

list.remove(i); // where i = 5 - 1, as the first element is 0.

if you do

list.remove(new Integer(i));

it will look for the object you just created, which doesn't exist in the list, so as you say it does nothing.

In short, don't confuse int for Integer as these are different types.

Ideally, the API would have a different method like removeAt(int) to remove this potential source of confusion, but it's too late to change it now.


When would this work? Consider the following.

List<Integer> ints = new ArrayList<>();
ints.add(4);

List<MyType> list = (List) ints; // it works but don't do this.
boolean wasRemoved = list.remove(new Integer(4)); // true!!
Sign up to request clarification or add additional context in comments.

4 Comments

I would maybe say that new Integer(i) can't exist in the list (rather than simply doesn't), because of generics.
@AndyTurner actually it could exist if you had used generics incorrectly.
Thanks, this made me realize my mistake. (which basically was that I first select an visual element, remove it and then try to remove the data behind the element with the now non-existant selection...)
@WorkHardDieSoon unfortunately its a reasonably common mistake to make.
3

java.util.List#remove(int) should work, just don't forget that lists are zero indexed, i.e. 5th element is removed calling list.remove(4)

Comments

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.