-2

I have unsolvable task, I have task, where i have insert random number to array. The user can choose if array is 1D, 2D, 3D,size of array is optional . I tried everything but withot success. I can not use ArrayList. Thank you for help.

double[] array= new double[size];
for ( int i;i<dimensional;i++)
{
 double[] array= new double[size];
}

Edit: I mind if is effective way to create array with 1D and then add to this array one or more dimension.

  • 1
    Simplest solution could be using splitting your code into few `if` sections each handling different dimension. So if user selected 2D use code like `if (dimensions == 2){double[][] = new double[size1][size2]; for([i in dimension1])for([j in dimension2]) array[i][j]=randomValue}`. Create similar code for other cases. – Pshemo Jan 07 '16 at 14:14
  • 1
    I'm not quite following what the actual question is. What problem are you trying to solve? How to write a method that accepts a parameter that is an array with an unspecified number of dimensions? How to *create* an array with a number of dimensions specified by the user? Something else? We expect specific questions here. – John Bollinger Jan 07 '16 at 14:21
  • You "tried everything"? Please, go ahead, and show what exactly have you tried. – Dima Jan 07 '16 at 14:33
  • Possible duplicate of [Is it possible to dynamically build a multi-dimensional array in Java?](https://stackoverflow.com/questions/3104504/is-it-possible-to-dynamically-build-a-multi-dimensional-array-in-java) – Md. Abu Nafee Ibna Zahid Mar 17 '18 at 06:24

3 Answers3

0

Multi-dimensional arrays in java are essentially just arrays of arrays. The user provides the number of dimensions and sizes at runtime so you need to dynamically build this array at this point. It's a strange problem, and not one that you would try to solve with arrays in production code, but nevertheless it should be possible. Try the accepted answer for this question, it seems like a pretty good attempt.

Community
  • 1
  • 1
ewanc
  • 1,284
  • 1
  • 12
  • 23
0

So, an "unsolvable" task ... well, as long as you work with primitive types and the dimension can be theoretically any number (only limited by memory available), you may be right.

You can however use some kind of object (Java is an object-oriented language) and solve the task rather easily. Basically, you might want a tree structure with nodes. You can even write a constructor that sets fixed sizes for every level and give no direct accessors to the array as a whole. Start with:

class Node {
    double payload;
    Node[] children;
}

I don't really understand what do you want to do with that, but it pretty much fits the idea of N-dimensional array.

Vlasec
  • 5,500
  • 3
  • 27
  • 30
0

Another solution: Make the array one-dimensional, but using the sizes of the individual dimensions, you can calculate the correct index. Of course, it will require you to handle the logic.

E.g. in a 2D array of 3x3 size, you can use a 1D array of 9 size and use first three indexes for first row, next three for second row, last three for third row. You can then use a cycle like this:

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        //arr[i * 3 + j] = ...
    }
}
Vlasec
  • 5,500
  • 3
  • 27
  • 30