-1

I want to iterate through some kind of list, checking if its elements meet a property, and if they don't deleting them from the array. What I've thought is something like this:

int index = 0;
for(int i = 0; i < list.size(); ++i) {
    if(list.isProperty()) list.delete(index) //We delete the element at list[index]
    else ++index;
}

Maybe those aren't the real methods of the list interface in java, but they're quite self-explanatory.

Is this a good approach? Which data structure would fit best if I have to run this operation many times? I don't think an arrayList would work as I'd have to be moving around elements each time I delete and I can't ensure the elements I'll remove are in the head or the tail of the list either.

Setzer22
  • 1,589
  • 2
  • 13
  • 29
  • 2
    Doubly-linked lists have O(1) deletion times as it's simply setting the links differently. – nanofarad Aug 13 '13 at 14:04
  • Is there a doubly-linked list implementation in the java libraries? I wasn't able to find it. I was actually looking for something like that. Like the std::list from C++ – Setzer22 Aug 13 '13 at 14:22
  • [LinkedList](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html) is actually doubly-linked on cursory examination of the javadoc. – nanofarad Aug 13 '13 at 14:23
  • Oh, my bad, I just thought linked list was just linked in one direction (like a stack or a queue), not both. Thank you. – Setzer22 Aug 13 '13 at 14:25
  • It's not really as much of being a stack or queue as it is differences in internal structure. – nanofarad Aug 13 '13 at 14:28

2 Answers2

2

You can achieve it using iterator.Without having concurrent modification exception.

Say your list consists of object A

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

Iterator<A> iterator = list.iterator();
while (iterator.hasNext()) {
   A current = iterator.next();   

   if(current.isProperty()) {
   iterator.remove();;
  }
}
Bren
  • 2,148
  • 1
  • 27
  • 45
  • 1
    +1 You can also use a for loop instead of a while loop to limit the scope of the Iterator – Peter Lawrey Aug 13 '13 at 14:10
  • Just out of curiosity, that is something i wonder. Is there any advantage having iterator scope narrower ?@PeterLawrey – Bren Aug 13 '13 at 14:12
  • 1
    It means you don't accidentally use it later for something you didn't intend, or if you do use it later its only because you intended to do that. If you are in the habit of limiting scope, it means you only have a wider scope when this really means something. – Peter Lawrey Aug 13 '13 at 14:14
1

You should remove an element from a List using an Iterator. You can use this with ArrayList.

List<YourDataType> yourList = new ArrayList<YourDataType>();
Iterator<YourDataType> it = yourList.iterator();
while (it.hasNext()) 
       it.remove();

With this you can use if-else to specify the element, which should be removed.

This should give you some hints, why you should use an Iterator.

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79