6

Is there any real use case for parallel arrays in Java? It seems too cumbersome to maintain N arrays which are interrelated.

Example:

int  ages[]   = {0,          17,        2,          52,         25};
String names[] = {"None",    "Mike",    "Billy",    "Tom",      "Stan"};
int  parent[] = {0,          3,         1,          0,          3};

I can just create a class Person and store objects in one single array. Will be little more expensive, but much easy to use right?

zengr
  • 38,346
  • 37
  • 130
  • 192
  • 2
    A couple of reasons that probably aren't particularly relevant to Java but might be worth mentioning are (1) a primitive array like `float[]` has better cache performance because the values are contiguous in memory and (2) can possibly take advantage of SIMD instructions and other vectorizing optimizations. – Radiodef Aug 09 '18 at 19:59

4 Answers4

12

The only real advantage of parallel arrays in Java is as an (IMO extreme) measure to reduce object allocation and / or heap usage. For a large enough collection of objects, 3 arrays will occupy less space AND use fewer objects than a single array of instances of some custom class.

This approach is normally a bad idea, since it makes your code a lot more fragile. Creating and using a custom class is the best approach in most situations.

Note: the Performance Tips for Android advise otherwise, but those tips are primarily focused on reducing the frequency / impact of GC pauses on user experience. And even that advice is caveated in a couple of ways.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 4
    Yes, this is something to keep in mind in Android development. Not all Android devices are equal, and mobile phones in general have limited resources. To quote from the Performance Tips from the Android Developers reference, `"If you need to implement a container that stores tuples of (Foo,Bar) objects, try to remember that two parallel Foo[] and Bar[] arrays are generally much better than a single array of custom (Foo,Bar) objects."` http://developer.android.com/training/articles/perf-tips.html – loeschg Jan 07 '13 at 14:44
11

Parallel arrays are a holdover from languages like Basic (the original one) that had no data structures other than arrays. You should define objects as you suggest, instead.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
1

Create Class/Object representation only if what you are trying to represent can be better explained as an object.

Hash Maps are another solution. For example, instead of differnt arrays for ages and names, if the names are unique, you can use the name as keys and age as the value.

Sujoy
  • 8,041
  • 3
  • 30
  • 36
  • 2
    Maps are almost as bad as parallel arrays. – Stephen C Mar 18 '11 at 03:28
  • @stephan: well i agree, but sometimes, i prefer to use maps when things are simple :) – Sujoy Mar 18 '11 at 05:07
  • I'll revise that. In some respects, Maps are worse; e.g. they are inevitably slower and more memory hungry than both parallel arrays and the preferred approach (custom classes). Besides the Map approach gets cumbersome if you have more than two attributes to associate ... as in this case. – Stephen C May 11 '11 at 08:03
1

Classes are nicer, but if the use case is narrow, e.g. one method body, it's wasteful to create one. If you have a Pair class, you could specialize it for the purpose without creating a new class.

Community
  • 1
  • 1
Vance Maverick
  • 812
  • 6
  • 4