In Java: Don't Use Arrays

Published: 2013-02-06

If you are a programmer and you are working in Java, I have a message for you: Do not use Arrays!

When you are programming, you always have to handle some data. And to be able to handle it, you need some data structure. Some of the most basic data structures possible to handle a dynamic (say unknown at compile time) amount of data are arrays. And arrays get the job done, no problem there. But they come with several severe shortcomings:

  • They are not really dynamic. You have to know in advance how big (at most) they gonna be. Or else inserting and removing data from arrays is a pain.
  • Arrays are built-in into the language. Thus they have some specialties normal objects (like e.g. Lists) do not have. You cannot extent them. You have to specially treat them when using reflection.
  • They have limited functionality. So if you need something like a synchronized array, a sorted array, an immutable array or an array without null values, you are lost.
  • They don't mix well with other Java data structures like Set or Map.
  • Arrays and generics don't mix well. For instance, you can't create a generic array like new T[0].
  • The hashCode function is the native function of the Object. Which means it returns the memory address of the object. Which is random in a non-deterministic fashion. And since you cannot extent an array, you cannot change this. Which means that this will likely make your program executions non-repeatable. Which is a pity for debugging.

The punchline? Do not use arrays in the first place. Use Java Collections, like ArrayList. There are very few reasons to use arrays. They are usually used for interacting with hardware and low-level optimizations. But because of the nastiness on low-level programming and optimizations, I recommend avoiding that altogether. Also, arrays can use primitives without boxing. But eventually all points come down to the following: ArrayLists use more space and are slower than plain arrays. But in like 99% of the programs, this doesn't matter. First write clean code, then use a profiler and only—repeat only—when you identified a performance bottleneck, optimize. And that doesn't necessarily mean you need to introduce an array.

But arrays are taught in class extensively. They are the first data structure anybody learns about, so it is natural for a novice programmer to think they are the first choice. They are taught for several reasons:

  • It is the way it used to be (historical reason).
  • They are common in almost every language, whereas e.g. ArrayList is highly Java specific.
  • They are easy to understand and interact with and thus make a nice show case for small example code.

But I am not the first one to recognize. For instance at a JavaOne On-line Technical Sessions from 2007 (!) I found a remark in Naftalins Java Technology Generics and Collections: Tools for Productivity: He says that in his book they've

... questioned whether there really is that much extensive use left for arrays in the Java language.

Other languages like Python do not have arrays at all (at least not in the primitive sense that Java does). And I personally consider this a good thing.

I'd love to read your response.
Mail me: moc.sthgisni-edamdnah@jrelsseor