0

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

2
  • It depends what you're doing and how you're using the collection. Commented Dec 7, 2011 at 2:10
  • 1
    @Mark Estrada - You can use remove(object) method. In that case you have to override equals and hashCode method in DumbBean. Commented Dec 7, 2011 at 2:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.