blekz
blekz

Reputation: 11

Extending collection without class body

I need in my program collection in collection in collection. So something like this:

ArrayList<ArrayList<ArrayList<String>>>

You can see this does not look good. Mainly when i am using a lot of generics. So i created something like this:

public class ThreeDimensionArray extends ArrayList<TwoDimensionArray> { }

class TwoDimensionArray extends ArrayList<ArrayList<String>> { }

is this solution bad in some way or is it ok?

Upvotes: 0

Views: 119

Answers (1)

Tim B
Tim B

Reputation: 41188

It's not great, but it is ok. It's rather masking what you are doing - and its a bit wasteful as you are creating a concrete class to define something that type erasure would have turned into a standard List at compile time.

Really you should be using List rather than ArrayList and the diamond operator, both changes will make the original tidier:

List<List<List<String>>> 3dList = new ArrayList<>();

If you do go down the defined class route at least use generics -

class TwoDimensionArray<T> extends ArrayList<ArrayList<T>> { }

Then you can use it for multiple types.

Upvotes: 1

Related Questions