tags:

views:

180

answers:

7
+8  Q: 

Stacking generics

Is this bad practice?

ArrayList<ArrayList<ArrayList<Double>>> list = new ArrayList<ArrayList<ArrayList<Double>>>();
+2  A: 

Well, do you need to have a List whose elements are Lists whose elements are Lists? We have no idea what it is you are trying to accomplish unless you tell us.

However, using ArrayList directly rather than List is indeed a bad practice.

matt b
You're absolutely right, thank you. No idea why I didn't do List to begin with.
Earl Bellinger
+1  A: 

Depends on how you intend to use this. Perhaps you could encapsulate the two dimensional list and end up with a List<TwoDimensionalList<Double>>. Presumably it would have operations such as TwoDimensionalList.get(int i, int j) to get an element in the jth position of the ith list.

edit: if it's not a list of two dimensional lists, but rather a three dimensional list, then of course you want a ThreeDimensionalList. (and if your list's dimensions are fixed, you could implement this internally with a single array(list) where element (i,j,k) is located at position i*dim1 + j*dim2 + k*dim3).

wds
+6  A: 

It is a three-dimensional matrix based on ArrayList. Doesn't look nice, but that's how we have to write it.

An alternative could be:

List<List<List<Double>>> list = new ArrayList<List<List<Double>>>();

which is a bit shorter and usually OK as in most cases your just interested in the interface methods.

So if you need a resizable threedimensional matrix data structure, then this is a clean approach.

Andreas_D
+4  A: 

It would probably be a good idea to create a new class to handle the behavior you are trying to accomplish. I would create a class that uses an private ArrayList<...> (favor delegation over inheritance) and create necessary methods. If anything it should make things easier to read and understand.

Jerod Houghtelling
A: 

At the least, naming it more expressively, something like 3dList, would help. Preferred, for me, is to write a custom encapsulation of 2D/3D list, as others have suggested above.

Shikhar
Nice suggestion, but in Java, identifiers can't start with a number. Invent something better ;)
BalusC
ThreeDimensionalList? ThreeDList?
Willi
+3  A: 

This is not necessarily bad practice. It's just "unreadable". Have a bit of patience, in the upcoming Java 7 you're allowed to omit the cruft in specific generic types when constructing the parameterized type:

List<List<List<Double>>> list = new ArrayList<>();

This is called type inference.

As of now, if you can live with compiler warnings, you can also just do so:

List<List<List<Double>>> list = new ArrayList();
BalusC
I think you could also create a factory method looking something like `public static <E> List<E> createArrayList()` and then invoke that instead of the constructor. The type inference will automagically determine what <E> is based on the assignment type.
Mike
or just use Guava's `Lists` class and replaced the constructor call with `Lists.newArrayList()`. This does exactly what Mike proposed.
Willi
+3  A: 

yes. most likely your code is better off with double[][][]

irreputable
This is probably a better choice if you're going for a three-dimensional array of doubles since an ArrayList of ArrayLists of ArrayLists of Double objects is likely to take up much more space if it's not a sparse array.
Alan Krueger
In general, you're correct. Java programmers routinely underestimate the memory overhead that objects introduce. If, however, you need your matrix to be resizable on the fly, and you know ahead of time that the size variance will be difficult to predict up front, then you might need to use the "stack o' generics" approach. Especially since, unlike C, arrays can not be dynamically resized in Java.
rtperson