Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I want to know if there is a difference in performance if I use a primitive array and then rebuild it to add new elements like this:

AnyClass[] elements = new AnyClass[0];

public void addElement(AnyClass e) {
    AnyClass[] temp = new AnyClass[elements.length + 1];
    for (int i = 0; i < elements.length; i++) {
        temp[i] = elements[i];
    }
    temp[elements.length] = e;
    elements = temp;
}

or if I just use an ArrayList and add the elements.

I am not certain that is why I ask, is it the same speed because an ArrayList is build in the same way as I did it with the primitive array or is there really a difference and a primitive array is always faster even if I rebuild it everytime I add an element?

share|improve this question
1  
When you have well tested implementation, why would you like to implement your own? – Nambari Oct 30 '13 at 18:15
    
Its just about performance I am asking myself if I can improve it using primitive types – Nickolaus Oct 30 '13 at 18:16
    
This would be extremely easy to implement both solutions and run a benchmark test comparing the speed between the two. – ricksuggs Oct 30 '13 at 18:17
    
Please fix the 1st statement. – PM 77-1 Oct 30 '13 at 18:20

6 Answers 6

up vote 4 down vote accepted

ArrayLists work in a similar way but instead of rebuilding every time they double there capacity every time the limit is reached. so if you are constantly adding to it ArrayLists will be faster because recreating the array is fairly slow. So your implementation could use less memory if you are not adding to it often but as far as speed goes it will be slower most of the time.

share|improve this answer
    
thanks your answer made it pretty clear why the ArrayList is the better solution (generally spoken), but if I know that my array will be in a range between a min and a max size could I improve the performance if I allocate a fixed number of elements (eg. 100) if the array is at its maximum size? – Nickolaus Oct 30 '13 at 18:23
    
If your range was extremely tight yes. but for a range of 100 no. With the new implementation you would have to resize 100 times, that would be very expensive. you can tell the ArrayList to start at a size of 100, so it would only have to resize once to get to a size of 200. – Todoy Oct 30 '13 at 18:27
2  
@Nickolaus: The proper solution is to create a pre-sized ArrayList using the other constructor. – maaartinus Oct 30 '13 at 18:28
    
1. the implementation above was only written by hand it is not in my code it should only be a quick example of what I mean – Nickolaus Oct 30 '13 at 18:29
    
2. if I have a presized ArrayList the size still gets doubled and not expanded by a fixed value – Nickolaus Oct 30 '13 at 18:30

When an ArrayList resizes it doubles itself, so that you are not wasting time resizing each time. Amortized, that means that it doesn't take any time to resize. That's why you shouldn't waste time recreating the wheel. The people who created the first one already learned how to make one more efficient and know more about the platform than you do.

share|improve this answer

In a nutshell, stick with ArrayList. It is:

  • widely understood;
  • well tested;
  • will probably be more performant that your own implementation (for example, ArrayList.add() is guaranteed to be amortised constant-time, which your method is not).
share|improve this answer

ArrayList will also use internally Array Only , so this is true Array will be faster than ArrayList. While writing high performance code always use Array. For the same reason Array is back bone for most of the collections. You must go through JDK implementation of Collections. We use ArrayList when we are developing some application and we are not concerned about such minor performance issues and we do trade off because we get already written API to put , get , resize etc.

share|improve this answer
  • There is no performance issue in both Arrays and ArrayList.
  • Arrays and ArrayList are index based so both will work in same way.
  • If you required the dynamic Array you can use arrayList.
  • If Array size is static then go with Array.
share|improve this answer

Your implementation is likely to lose clearly to Java's ArrayList in terms of speed. One particularly expensive thing you're doing is reallocating the array every time you want to add elements, while Java's ArrayList tries to optimize by having some "buffer" before having to reallocate.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.