Reputation: 48406
I'm new to Java (day 2), so please inform and forgive me if this doesn't make sense.
I have a two dimensional array, and I want to define a new array that consists of all of the first dimension for a specific location in the second dimension (example below). The only way I can think to do this is with a for
loop, something like this:
double[][] twoDimArray = new double[5000][200];
// some code defining twoDimArray
double[] oneDimArray = new double[5000];
for (int i=0; i<5000; ++i) {
oneDimArray[i] = twoDimArray[i][199];
}
This works just fine, but is there a cleaner way to do this? Perhaps something like this:
double[] oneDimArray = twoDimArray[][199];
Thanks.
Upvotes: 3
Views: 9853
Reputation: 117675
Try this:
Double[] oneDimArray = new ArrayList<Double[]>(Arrays.asList(twoDimArray)).get(199);
Upvotes: 1
Reputation: 21214
There are no multidimensional arrays in Java as such, there are just arrays of arrays. But even if there was, dimensions are either stored in row major or column major order. Depending on this, either cells in the same 'row' or in the same 'column' (i.e. the first or second dimension) are in memory consecutively and can directly be assigned to a one dimensional array.
Since you are trying to extract across the dimension which is stored consecutively, there is no better way than the for loop. If, on the other hand, you would have wanted all the elments from first position 199, you could just do double[] oneDimArray = twoDimArray[199];
.
So if extraction amongst this dimension is your common use case, it would make sense to store the data with the dimensions swapped, i.e. you would do:
double[][] twoDimArray = new double[200][5000]; // dimensions are swaped here
// some code defining twoDimArray (with swapeddimensions)
double[] oneDimArray = twoDimArray[199];
Upvotes: 4
Reputation: 82599
What you're doing is the most effective way. There are no shortcuts (like some languages might have newarr = oldarr[:][199];
or something). If you had to do it more than a couple times, I would recommend making a method to do it.
double[] acquireRow(double[][] orig, int row) {
double[] ret = new double[orig.length];
for(int i = 0; i < ret.length; i++) {
ret[i] = orig[i][row];
}
return ret;
}
Upvotes: 1
Reputation: 9237
twoDimArray[0]
is actually the first dimension of your array if I understand your question correctly.
Imagine a 2D array as a bunch of rows of 1 D arrays. So if you have twoDimArray[5000][200]
then you have 5000 rows (1D arrays) that have 200 spots each.
so twoDimArray[2]
for example is actually the third row (1D array) , i.e a double[]
EDIT: Since in java there are arrays of arrays and not exactly 2D arrays, you might want to reverse the order of definition of your array to be array[200][5000] so that you can access the dimension you want easily in one line.
Upvotes: 2
Reputation: 8020
I think you have to specify both dimensions while creating an array. If you don't want to specify that value because you are not sure of it, read on "lists" data type.
Upvotes: 0