Although the answers proposing to use ArrayList do make sense in most scenario, the actual question of relative performance has not really been answered.
There are a few things you can do with an array:
- create it
- set an item
- get an item
- clone/copy it
General conclusion
Although get and set operations are somewhat slower on an ArrayList (resp. 1 and 3 nanosecond per call on my machine), there is very little overhead of using an ArrayList vs. an array for any non-intensive use. There are however a few things to keep in mind:
- resizing operations on a list (when calling
list.add(...)
) are costly and one should try to set the initial capacity at an adequate level when possible (note that the same issue arises when using an array)
- when dealing with primitives, arrays can be significantly faster as they will allow one to avoid many boxing/unboxing conversions
- an application that only gets/sets values in an ArrayList (not very common!) could see a performance gain of more than 25% by switching to an array
Detailed results
Here are the results I measured for those three operations using the jmh benchmarking library (times in nanoseconds) with JDK 7 on a standard x86 desktop machine. Note that ArrayList are never resized in the tests to make sure results are comparable. Benchmark code available here.
Array/ArrayList Creation
I ran 4 tests, executing the following statements:
- createArray1:
Integer[] array = new Integer[1];
- createList1:
List<Integer> list = new ArrayList<> (1);
- createArray10000:
Integer[] array = new Integer[10000];
- createList10000:
List<Integer> list = new ArrayList<> (10000);
Results (in nanoseconds per call, 95% confidence):
a.p.g.a.ArrayVsList.CreateArray1 [10.933, 11.097]
a.p.g.a.ArrayVsList.CreateList1 [10.799, 11.046]
a.p.g.a.ArrayVsList.CreateArray10000 [394.899, 404.034]
a.p.g.a.ArrayVsList.CreateList10000 [396.706, 401.266]
Conclusion: no noticeable difference.
get operations
I ran 2 tests, executing the following statements:
- getList:
return list.get(0);
- getArray:
return array[0];
Results (in nanoseconds per call, 95% confidence):
a.p.g.a.ArrayVsList.getArray [2.958, 2.984]
a.p.g.a.ArrayVsList.getList [3.841, 3.874]
Conclusion: getting from an array is about 25% faster than getting from an ArrayList, although the difference is only on the order of one nanosecond.
set operations
I ran 2 tests, executing the following statements:
- setList:
list.set(0, value);
- setArray:
array[0] = value;
Results (in nanoseconds per call):
a.p.g.a.ArrayVsList.setArray [4.201, 4.236]
a.p.g.a.ArrayVsList.setList [6.783, 6.877]
Conclusion: set operations on arrays are about 40% faster than on lists, but, as for get, each set operation takes a few nanoseconds - so for the difference to reach 1 second, one would need to set items in the list/array hundreds of millions of times!
clone/copy
ArrayList's copy constructor delegates to Arrays.copyOf
so performance is identical to array copy (copying an array via clone
, Arrays.copyOf
or System.arrayCopy
makes no material difference performance-wise).