0

I am using Java. I want to add to the start of an Array. Would it be more efficient to move all variables up one space in the array, leaving one spot for a new variable to be added in index 0, or to just use an ArrayList?

I am aware an ArrayList will move the values for me, but I have heard that they are very inefficient, is this true?

Are there any other APIs that will do this efficiently?

4
  • possible duplicate of Java Performance - ArrayLists versus Arrays for lots of fast reads Commented Dec 13, 2013 at 10:53
  • Are you going to add to the first index only once? or a lot of times per list? A LinkedList is much more efficient for adding elements at the head - at the cost of not having a random access to all elements in the list. Commented Dec 13, 2013 at 10:54
  • @ShivanRaptor How is that a dupe? The linked question is about a lot of reads. He asks about writing an element to the head of the list. Commented Dec 13, 2013 at 10:55
  • I will be adding to the front lots of times, I think Linked list sounds like the best choice! Commented Dec 13, 2013 at 11:20

5 Answers 5

2

Apart from the method call overhead and some small maintenance cost, ArrayList is no more inefficient than copying array elements yourself. Some implementations of ArrayList may even be faster at moving data, by allowing the list to start somewhere else in the backing array than at index 0, as ArrayDeque does.

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

Comments

2

Neither would be efficient, because each insertion at the beginning needs to move what you've added so far. This means that inserting N elements takes O(N2) time, which is rather inefficient.

LinkedList<T>s are better for situations when you need to insert at the beginning of the list. However, they have memory overhead, and do not allow fast lookup based on the index.

If you do not need to use your list until after all elements have been inserted, you may be better off inserting elements at the back of the list, and then reversing the list before starting to use it.

Comments

0

ArrayList also uses Arrays internally to store the data. But, Sun/Oracle added a fastest algorithm to add the item in index 0 and move the items starting from index 1. So, better use the ArrayList for simpler coding, But if you can tweak a better algorithm, then go for Array.

Comments

0

If you would be adding to the first index very frequenlty, it will be very expensive as it needs to relocate all the indices from 1 to end of the array i.e it will resize it itself to adjust a new element at the top. LinkedLists provide better performance in such cases but they do not implement the Random Access behaviour .

Comments

0

ArrayList provides enough performance for normal usage, and what's even more important - they are safe. So you don't need to worry about getting out-of-bounds, null-pointers etc.

To make it "faster" you can, for example, get rid of ArrayList's checking capacity etc., but then you are making your code unsafe, which means you must be sure you are setting the right parameters, because if not you will be getting IndexOutOfBounds etc.

You can read a very interesting post about Trove - using primitive collections for performance, for more information.

But 99 times out of 100, there is no real need. Remember and repeat after me:

Premature optimization is the root of all evil.

Besides, I really recommend checking out the JDK source code yourself. You can learn a lot and, obviously, see how it's made.

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.