Reputation: 9191
I happen not to give any thoughts about this but I have been doing this for quite a while in a project that I am working on.
public class Test {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new DumbBean("A"));
list.add(new DumbBean("B"));
list.add(new DumbBean("C"));
System.out.println(list);
list.add(1, new DumbBean("D"));
System.out.println(list);
list.remove(2);
System.out.println(list);
}
}
It actually works in my case. I do a lot of insert/delete/add on a java arraylist.
I happen to think if I am overlooking something or is there a better alternative to this. (...another collection?)
Thanks
Upvotes: 0
Views: 3811
Reputation: 8204
If you find yourself appending to the end of the list a lot, I'd go with the ArrayList. If you find yourself inserting and removing in the middle, I'd go with a LinkedList.
If you deal with lists that are less than length 20, I'd forget about this entirely. Unless you have performance metrics that indicate this is a bottleneck for your program, this isn't even worth your time.
Upvotes: 1
Reputation: 800
Here is a good comparison of data structures:
http://en.wikipedia.org/wiki/Comparison_of_data_structures
It's good that you already know that you will have a lot of inserts and deletes as this will help you decide on which is better. Remember, some structures you will need to search before deleting or inserting in place.
Upvotes: 0