RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build an Embedded Array Language in Java

This article describes a generalized system for dealing with multidimensional arrays of data. It lets you transform and extract slices of data along various axes with very little code, in the style of the programming language APL. And you're not limited to numerical data; you can generalize systems such as this to any kind of tabular data.


t's really not surprising that the first "killer app" in the world of personal computing was the spreadsheet. After all, a table filled with numbers is one of the simplest and most useful metaphors for displaying and analyzing data.

Arrays of numbers are a cornerstone of numerical programming. They are so central to it that a number of languages (notably APL and its successors, J and K) have been created that specialize in multidimensional arrays. Proponents of these languages claim that array languages allow for much cleaner, comprehensible code.

If you've ever done any kind of financial programming, you've dealt with multidimensional arrays of numbers. Dealing with high-dimension arrays can be tricky, because it's hard to picture the data structures, and hard to draw diagrams. And code itself can be hard to read. Just think back to the last triply-nested for-loop you wrote—you remember—the one that took up three screens of code.

The advantages are great, however. Even though your numerical data structures might have an unwieldy number of dimensions, they have the advantage of homogeneous axes—that is, you can rotate the array through higher-dimensional space, extract lower-dimensional slices, and manipulate and display them. Each axis is similar to every other axis, and so the various axes can all be handled by the same set of routines.

This article discusses the design and implementation of a library for multidimensional arrays, and how it can make programming simpler.

A Note on Generics
For those of you interested in learning about the Java Generics feature of JDK 1.5, note that the library described in this article does use them. The implementation of the Cube relies heavily on the new Generics feature of JDK 1.5. The Cube class is parameterized by T, the type of the individual cells of the cube. One nice aspect of this is that you don't have to decide beforehand how many dimensions the Cube has—the constructor will take an array of any dimension and recursively construct the cube from it. I have to admit that I was surprised that this worked; it seemed a little magical. The code for that constructor should be enlightening.

As this was the first time I had used this feature in Java, I was surprised at how rarely I needed to specify the type parameter "T" in various parts of the code; the compiler seemed able to infer the type of T pretty often.

The Cube Class
Our code centers around a class called Cube. A Cube is a multidimensional array. The name is a bit misleading, since a cube is three-dimensional, but rest assured that the Cube class handles every dimension from 1 on up.

A Cube is a multidimensional array. In Java, that also means it's a nested array—an array of arrays, an array of arrays of arrays, and so on. I chose the name "Cube" to avoid any implication of a tree concept, where each nested part can be a different shape; rather, a cube is a regular grid of any dimension.

First, you'll see how to use a Cube, and after that, you'll see how it's implemented.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date