0

I need a 5-dimensional data structure in Java with "double" as type for all the cells. For 3 of dimensions I know the size, so it fits to array category. But for 2 of dimensions I don't know the size beforehand; looks like ArrayList. I have been able to manage the combination for 3 dimensions in the past:

ArrayList(Double)[][] prXifY = (ArrayList(Double)[][]) new ArrayList[m][n];

But despite long hours working on it (and through search in the net), I wasn't able to scale it. I need something like this:

ArrayList(ArrayList(Double))[][][] prXiXjY = (ArrayList(ArrayList(Double))[][][]) new ArrayList(ArrayList<Double))[m][m][n];

When I tried the above code, it says: "Cannot create a generic array of ArrayList(ArrayList(Double))"

I will appreciate quick and complete answers.

By the way, this is my very first post ever. I tried my best to do a good job on searching beforehand and explaining the problem clearly. Comments on these matters are appreciated as well. :)

7
  • 3
    Why are you using parentheses? They aren't the right way to give a generic type argument. Commented Feb 20, 2015 at 22:24
  • Specifically, your types should read ArrayList<ArrayList<Double>> and not ArrayList(ArrayList(Double)). Commented Feb 20, 2015 at 22:25
  • I would suggest using Lists all the way down, since generics and arrays don't mix well, but it honestly looks like you should be splitting this up with a class at some level -- perhaps with a class containing a List<List<Double>>, rather than trying to nest five dimensions directly. Commented Feb 20, 2015 at 22:25
  • Wow! That was quick. Thanks. First I typed <> signs in the editor here, they didn't appear well in the preview, so I replaced them with (). I didn't do that in my code. Commented Feb 20, 2015 at 22:31
  • So I got two suggestions so far: list and split to class. Which one is better for this situation? Can anybody tell me please? Commented Feb 20, 2015 at 22:33

3 Answers 3

3

An ArrayList is an object, and instantiated differently than an array is. In general, to say that you want an ArrayList that holds doubles, you might use something like:

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

to specify that you have an ArrayList which holsts ArrayLists which hold doubles...

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

You see where this is going, I hope.

This just creates the top-level list - it doesn't create any of the cells themselves. For that, you'll need additional new statements. This is going to get messy fast, and you may want to stop and consider if there is a better way to store the data than in a 5-dimension array.

Sign up to request clarification or add additional context in comments.

1 Comment

I know. That's why I am asking for other solutions. Do you know how to do the task?
2

I think what you want would look something like this

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

as you can tell this looks insane and will be very hard to maintain. You should probably look at alternative methods of doing this.

1 Comment

I am asking about those alternative methods. Do you know one of them?
0

Multi dimensional arrays can be created like so:

    ArrayList<Double> oneDimensionalArrayList = new ArrayList<>();
    ArrayList<ArrayList<Double>> twoDimensionalArrayList = new ArrayList<>();
    ArrayList<ArrayList<ArrayList<Double>>> threeDimensionalArrayList = new ArrayList<>();
    ArrayList<ArrayList<ArrayList<ArrayList<Double>>>> fourDimensionalArrayList = new ArrayList<>();
    ArrayList<ArrayList<ArrayList<ArrayList<ArrayList<Double>>>>> fiveDimensionalArrayList = new ArrayList<>();

But I would definitely recommend considering whether a 5 dimensional array is what you require for the problem at hand; it smells like something is wrong

1 Comment

Nothing is wrong. I am implementing an algorithm for multidimensional object. It is very common in scientific problems. Seems like it is not easy to do that efficiently though. Because nobody has a solution/alternative to this problem, I am going to share with everybody how I got around it. because the size of those two dimensions are in a short range, I decided to go with 5-dim arrays and use the max possible size for those 2 dimensions. Please, stop posting that it is not the best way to do that, give me some good ideas that I don't have already. I will appreciate that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.