0

While I was browsing answers here, I read that

"Java does not have true 2-dimensional arrays; it has arrays of arrays, so x[rows][cols] is an array x of rows arrays of cols elements (i.e. x[rows] is an array of arrays)."

That seems like good news for me, as I want to create a single 1D array by copying a "row" from a 2D array. What is the correct syntax to call a specific array from P, my 2D array? Essentially:

new double[] row1 = P[row];

Or do I have to loop through the row I want to copy? I have tried the above code and several similar approaches. All receive an error of "Array dimension missing".

5
  • Have you tried using this (kind of) code? If yes what were your results? Commented Nov 6, 2016 at 16:12
  • @UnholySheep Yes sir, I have tried a few different things along those lines. They all result in a compiler message of "Array dimension missing". Commented Nov 6, 2016 at 16:14
  • Now that is more along the lines of how you ask on SO. So, why did you write new at the beginning? Why do you think that should be there? (It shouldn't and is the cause of your error) Commented Nov 6, 2016 at 16:16
  • @UnholySheep Errors gone. Thank you. I did it that way because I've never declared a new array without using the word new, and did what I do normally. I have edited the question to include the detail that I tried the code that I went through the effort of typing into the question initially, as well as the fact that it received an error message (surprisingly enough I wouldn't be asking if it had worked). I do understand that some people might not be able to arrive at that conclusion on their own. Should I delete my question, as it was a "typo", or do you think it might be helpful to others? Commented Nov 6, 2016 at 16:26
  • 1
    In your code you aren't declaring a new array though, it's a reference to another array (as explained in the answers below). That's why you don't need to new anything (it also means that any modifications you do in row1 will be also done in P[row]. If you were to create a copy that would not be the case.) Commented Nov 6, 2016 at 16:31

3 Answers 3

1

If you want a reference to a row, you can use double[] selectedRow = P[rowToSelect]. The new keyword in your code is misplaced. If you want a real copy or better known as deep copy, you can use:

double[] selectedRow = new double[P[rowToSelect].length];
System.arraycopy(P[rowToSelect], 0, selectedRow, 0, P[rowToSelect].length);`
Sign up to request clarification or add additional context in comments.

1 Comment

The "new" was the source of all the errors. Thank you for the additional information on "deep copy", that's great!
1

The easiest way is to use a reference, rather than a copy:

final double[] row1 = P[row];

However, if you need a copy, you can do this:

final double[] row1 = Arrays.copy(P[row], P[row].length);

This of-course assumes P is of type double[][] and row is an int which is within the valid index range.

1 Comment

@wesleyNeill Its the duty of the comment author to provide comments as comments and answers as answers. However, you can always ask the comment author to re-post their comment as an answer, and then upvote/accept that answer.
1

In your code, you are copying reference, instead of an array:

new double[] row1 = P[row];

In case of modification any of them, both would show the result.

To copy the array you can use static method copyOf from Arrays class:

double[][] source = new double[rows][cols];
double[] destination = Arrays.copyOf(source[rowNumber], source[rowNumber].length);

The other way of doing this is by using static method arraycopy from System class:

System.arraycopy( source[rowNumber], 0, destination, 0, source[rowNumber].length );

And the lastone is by invoking clone method:

double[] destination = source[rowNumber].clone();

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.