3

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.

0

5 Answers 5

4

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];
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

This doesn't seem to answer the question, though. I want to get twoDimArray[][199]. Is this possible?
Sorry if i didn't make it clear. Since in Java you have array of arrays (no exact 2D array) , if you are going to use that second dimension a lot , might as well redefine your array in opposite direction.
Or just make a method like what @TotalFrickinRockstarFromMars suggests
1

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;
}

Comments

1

Try this:

Double[] oneDimArray = new ArrayList<Double[]>(Arrays.asList(twoDimArray)).get(199);

Comments

0

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.

Comments

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.