Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding and removing elements to/from an arraylist?  RSS feed

 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to add and remove elements from/to an ArrayList whenever it meets a certain limit, e.g: 10. Whenever it meets the limit it should remove the first elements, i.e 0, and will add the new one..
I'm not quite sure how to do it...



Can someone please help?
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what you are looking for is not an ArrayList but a Queue. See here. An implementation of both List and Queue is LinkedList. poll() and offer() might be the methods you want to use.
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Christian, I think this is better handled by a Queue implementation. I recommend the ArrayBlockingQueue, because you can instantiate it with a fixed capacity, and the offer() method will return false if the queue is full, whereupon you can remove an element.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:I think what you are looking for is not an ArrayList but a Queue. See here. An implementation of both List and Queue is LinkedList. poll() and offer() might be the methods you want to use.


Thank you for that information, i will look into it but is there a way i could do that with ArrayList?
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it like this..


 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also make sure you don't add or remove elements to/from the ArrayList while you iterate over it or you might get a ConcurrentModificationException. I'm mentioning this since I can't recognize from your code if you work with one or two lists (what is history?).
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:Also make sure you don't add or remove elements to/from the ArrayList while you iterate over it or you might get a ConcurrentModificationException. I'm mentioning this since I can't recognize from your code if you work with one or two lists (what is history?).


i meant array.remove(0);
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mammshean santiez wrote:
Christian Pflugradt wrote:Also make sure you don't add or remove elements to/from the ArrayList while you iterate over it or you might get a ConcurrentModificationException. I'm mentioning this since I can't recognize from your code if you work with one or two lists (what is history?).


i meant array.remove(0);

Then this will most likely give you troubles. The question is, why do you check the size on every element? If you remove the first element everytime an element is added to the list you should never have more than 10 entries, otherwise it might be better to remove items in a while(array.size() >= 10) loop. Sticking with your example, the code could be like this:

You also mentioned you wanted to add an item to the list when the limit is reached. If you require further help, now might be the time to reveal a bit of background information so we can get a better understanding of what you're actually trying to achieve.

P.S.: Before anyone else points it out. From java 1.5 on you should not list unparametrized collections anymore. Regardless of that you should also aim to implement against interfaces. Apart from that array might not be the best name for a structure that is not an array but a collection. So your first line could look like "List<Integer> arrayList = new ArrayList<>();" in Java 1.7 (in Java 1.5 and 1.6 you would also have to parametrize the instance) or you could use a lambda expression instead of your for loop in Java 1.8.
 
Stephan Crandego
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christian Pflugradt wrote:
You also mentioned you wanted to add an item to the list when the limit is reached. If you require further help, now might be the time to reveal a bit of background information so we can get a better understanding of what you're actually trying to achieve.


I need a queue or arraylist with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element and add the new one..
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mammshean santiez wrote:I need a queue or arraylist with a fixed size. When I add an element and the queue is full, it should automatically remove the oldest element and add the new one..

If you use only the add() method to add elements to your ArrayList (not set or add with index parameter etc.) you could just extend ArrayList and modify the add method to behave like required. This might work (untested):
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't extend ArrayList, because I think implementing a Collection implies that elements won't get lost upon performing an add() operation.

Instead, make a custom class that is composed of a List instead.
 
Peter Muster
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I wouldn't extend ArrayList, because I think implementing a Collection implies that elements won't get lost upon performing an add() operation.

Instead, make a custom class that is composed of a List instead.

That's why I would use a Queue instead but the question was if it was possible to implement this behaviour using an ArrayList. I agree that implementing your own List is a cleaner way but the effort is also higher than just mis-using an existing implementation to overwrite one method. The deciding factor would probably be the context. Is a clean approach desirable or should it be "quick and dirty"? Is the implementation used for a single use case? Is the thread opener the only developer using this structure or should it be available to others? And so on...
 
Stephan van Hulst
Saloon Keeper
Posts: 7807
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, my point was that you shouldn't extend any collection type, because in this scenario chances are high that you'd be violating the Collection contract. This is roughly what I would do:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!