It is pretty obvious that array[10] is faster than array.get(10), as the later internally does the same call, but adds the overhead for the function call plus additional checks.
Modern JITs however will optimize this to a degree, that you rarely have to worry about this, unless you have a very performance critical application and this has been measured to be your bottleneck.
Answer from TwoThe on Stack OverflowIt is pretty obvious that array[10] is faster than array.get(10), as the later internally does the same call, but adds the overhead for the function call plus additional checks.
Modern JITs however will optimize this to a degree, that you rarely have to worry about this, unless you have a very performance critical application and this has been measured to be your bottleneck.
From here:
ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.
In terms of performance Array and ArrayList provides similar performance in terms of constant time for adding or getting element if you know index. Though automatic resize of ArrayList may slow down insertion a bit Both Array and ArrayList is core concept of Java and any serious Java programmer must be familiar with these differences between Array and ArrayList or in more general Array vs List.
Java: Is Arraylist better than Arrays?
Array or List in Java. Which is faster? - Stack Overflow
java - are arrays faster than arraylist? - Stack Overflow
List vs Array vs ArrayList
Videos
I've been programming in Java for 10 months now (as a subject in school) and i was always curious whats the biggest difference between Areays and Arraylist. I know that if i had an Arraylist named 'list' and i wrote System.out.print(list) it will print all the things that the list contains. Something that Arrays can't do that easily. But whats the actually biggest difference?
I suggest that you use a profiler to test which is faster.
My personal opinion is that you should use Lists.
I work on a large codebase and a previous group of developers used arrays everywhere. It made the code very inflexible. After changing large chunks of it to Lists we noticed no difference in speed.
The Java way is that you should consider what data abstraction most suits your needs. Remember that in Java a List is an abstract, not a concrete data type. You should declare the strings as a List, and then initialize it using the ArrayList implementation.
List<String> strings = new ArrayList<String>();
This separation of Abstract Data Type and specific implementation is one the key aspects of object oriented programming.
An ArrayList implements the List Abstract Data Type using an array as its underlying implementation. Access speed is virtually identical to an array, with the additional advantages of being able to add and subtract elements to a List (although this is an O(n) operation with an ArrayList) and that if you decide to change the underlying implementation later on you can. For example, if you realize you need synchronized access, you can change the implementation to a Vector without rewriting all your code.
In fact, the ArrayList was specifically designed to replace the low-level array construct in most contexts. If Java was being designed today, it's entirely possible that arrays would have been left out altogether in favor of the ArrayList construct.
Since arrays keep all the data in a contiguous chunk of memory (unlike Lists), would the use of an array to store thousands of strings cause problems ?
In Java, all collections store only references to objects, not the objects themselves. Both arrays and ArrayList will store a few thousand references in a contiguous array, so they are essentially identical. You can consider that a contiguous block of a few thousand 32-bit references will always be readily available on modern hardware. This does not guarantee that you will not run out of memory altogether, of course, just that the contiguous block of memory requirement is not difficult to fufil.
Any performance difference will be negligible, especially if you initialize your arraylist with an initialCapacity. Write your code in whatever way makes it the most readable and maintainable, and try not to optimize stuff like this unless you've determined through testing that you are getting a significant performance hit from it.
Potential reasons to use ArrayList:
- useful methods (
contains, etc.) - implements
Iterable,Collection, andList, and can therefore be used in a lot of API calls that are interface-based. - even though you currently "know" that your collection's size can never change, life throws unexpected requirements at us. Why lock yourself into a pattern when there's no discernible advantage to be gained?
ArrayList gives you many features a raw array does not have. If you know the number of elements you can create an ArrayList of that size.
new ArrayList<String>(100);
If you are worrying about the difference in speed between an ArrayList and an array, you are worrying about the wrong thing. It is highly unlikely to be the bottleneck in your code. If it is, there is almost certainly a better answer than changing to an array.
Don't succumb to premature optimization. It'll wreak havoc on your code. Most things don't matter, only a few things do. You can only find those few things by profiling your code. Trying to make every part fast is a very ineffective way of making the whole fast. Keeping a clean, simple design is much more effective. That will give you the necessary seams for introducing optimizations in the one or two places they're actually needed.