Hello.

I want to ask you all how to remove an element from an arraylist?
Let's say the arraylist is made of the Integer elements: 23, 43, 40, 10;
I want to remove the number 43. How do I do that?
And don't tell me arraylist.remove(1); because this removes the element found at the given index, I don't want to remove elements by their index, because the arraylist will change all the time.
Want to do something like: arraylist.remove(element>Integer>43);

Please help. Thx

Use Iterator class

import java.util.*;

public class ArrayTest{

	public static void main(String[] args){
		List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(23, 43, 40, 10));
		System.out.println("BEFORE DELETE " + numbers);
		for (Iterator<Integer> iterator = numbers.iterator(); iterator.hasNext();) {
		  Integer number = iterator.next();
		  if (number == 43) {
		      iterator.remove();
		  }
		}
		System.out.println("AFTER DELETE " + numbers);
	}
}

Use Iterator class

import java.util.*;
public class NewClass1{

	public static void main(String[] args){
	List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(23, 43, 40, 10));
		System.out.println("BEFORE DELETE " + numbers);
                numbers.remove(new Integer(43));
		System.out.println("AFTER DELETE " + numbers);
	}
}

We can remove particular object without using iterator also, In case if we dont know the size/contents of arraylist, then it is better to use iterator to avoid exception. So you have used iterator. Am i right peter_budo ? Or any other advantage is there to use Iterator in this scenario?

Edited 5 Years Ago by Muralidharan.E: n/a

@Muralidharan.E

"We can remove particular object without using iterator also, In case if we dont know the size/contents of arraylist, then it is better to use iterator to avoid exception. So you have used iterator. Am i right peter_budo ? Or any other advantage is there to use Iterator in this scenario?"


Your approach would work properly only in case that you have no duplicates in collection so it would be safe to use in combination of any class implementing Set where there would be no duplicates. However in "raw" manner as you have now if you add second element ( Arrays.asList(23, 43, 40, 10, 43) )with same value you will only remove first instance

Comments

Is this possible to remove element by its index value?

That is easy, after you made your ArrayList object (yourArrayList in this case) just do:

yourArrayList.remove(index) // index of tipe int

Is this possible to remove element by its index value?

Possible but recommended only in "small dozes" as you may easily introduce bugs to your logic

That is easy, after you made your ArrayList object (yourArrayList in this case) just do:

yourArrayList.remove(index) // index of tipe int

This is time consuming in case if you have multiple elements to remove, you would better to either use Collection.removeAll(Collection<?> c) or perhaps use of Maps

So, how would you remove a duplicated number in an array list without using "Collection.removeAll(Collection<?> c) " out of interest?

So, how would you remove a duplicated number in an array list without using "Collection.removeAll(Collection<?> c) " out of interest?

Out of interest, put in some data and we may play around ;)

So, how would you remove a duplicated number in an array list without using "Collection.removeAll(Collection<?> c) " out of interest?

while (numbers.remove(new Integer(43)));

Thanks, I suppose you would have to use the index to only remove one of the duplicated values which would depend on the case anyway.

I'm not a programmer, I'm the analyst so don't have an ide at work :o

Thanks, I suppose you would have to use the index to only remove one of the duplicated values ...

Yes I think so (unless you want to remove the first occurrence).

String[] arr={"a","b","c","d"};
List<String> list = Arrays.asList(arr);
list.remove(2);

but when I run this code it shows UnsupportedOperationException. could any one explain me

The List interface shows the remove methods as optional, and adds the following for all the remove methods:

Throws:
UnsupportedOperationException - if the remove operation is not supported by this list

Arrays.asList has the following documentation

public static <T> List<T> asList(T... a)

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.

This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

So it looks pretty clear that the List returned from asList will not support List's optional remove methods

ps If you want to continue using the asList approach then you need to do something like

ArrayList<String> list = new ArrayList<String>(Arrays.asList(arr));

Edited 5 Years Ago by JamesCherrill: ps

Comments
You beat me to the answer
This question has already been answered. Start a new discussion instead.