Skip to main content

Array vs ArrayList

Short answer

Use a plain array when you know the size up front and the elements are primitives, because it avoids boxing and a layer of indirection. Use ArrayList the moment the size is unknown or you want the collections API.

ArrayList is not a different data structure. It is an array with a length counter and a resize policy wrapped around it, which is why the costs look so similar. The differences that matter in an interview are boxing, resizing and which API you get, and all three follow from that one fact.

Side by side

Dimensionint[]ArrayList
LengthFixed at creation, finalGrows and shrinks as you add and remove
Element typePrimitives or objectsObjects only, so int becomes Integer
Access by indexO(1)O(1), one extra method call
AppendNot available, allocate and copyAmortised O(1), occasional O(n) resize
Insert or remove in the middleManual shifting, O(n)O(n), the shifting is done for you
Memory per element4 bytes for an int16 or more for an Integer plus a reference
APIArrays.sort, Arrays.fill, lengthsort, contains, subList, streams, size()

When to pick each

int[]

  • The size is known, as in a dp table of n + 1 entries or a 26-slot letter count.
  • The elements are primitives and the array is large, where boxing every int would triple the memory.
  • A two-dimensional grid, where int[][] is both faster and easier to read than nested lists.
See it step by step

ArrayList

  • The size depends on the input, which is most result collections.
  • You want contains, indexOf, removeIf or a stream without writing the loop yourself.
  • The method signature has to accept or return a List, which most library code expects.
See it step by step
The mistake to avoid

Calling remove on an ArrayList inside a for-each loop, which throws ConcurrentModificationException, and the fix is an iterator or removeIf rather than a copy. The other one is List.of and Arrays.asList: both return fixed-size views, so add and remove throw UnsupportedOperationException on something that looks like an ordinary list.

Questions people ask

Is ArrayList slower than an array?

For access by index the difference is one method call, which the JIT usually removes. The real cost is boxing: an ArrayList of Integer holds references to objects scattered on the heap, so iterating it misses the cache far more often than walking an int[]. On a tight numeric loop over millions of elements that is a measurable gap, and on anything smaller it is noise.

How does ArrayList grow?

When the internal array fills, it allocates a larger one, copies everything across and discards the old one. The growth factor is 1.5 in Java, so the copies get rarer as the list gets longer, and the average cost of add stays O(1). That averaging is what amortised means.

Which should I use in an interview?

Whichever makes the code shorter, and say why in one sentence. Fixed-size dp tables and grids as arrays, result lists as ArrayList. Interviewers care that you knew the size was fixed, not which one you typed.

Read next

Other comparisons