简体   繁体   中英

Can't print/obtain specific rows of a 2D array

let's say I have a multidimensional array which for simplicity's sake we'll say just holds numbers:

double[][] MDarray = new double[10][20]

So this will be 20 rows by 10 columns. With the way my program works, writing into these elements means that the final 10-15 elements of every row will likely be populated with just '0's, here is an example of what my MD array's would look like with populated elements:

[
[0, 1, 2, 3, 4, 5, 6, 0, 0, ..]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0, ..]
[0, 1, 2, 3, 4, 0, 0, ..]
.. etc

How does one access just a specific row? This MD array is supposed to be a datastructure holding the results to my program. There is a 'winner' from this program, so if the program decides that '7' is the winner, then row 8 of this array holds data fundamental to the completion of my program code.

Attempting to print results like this:

int bestResult = 7;
System.out.println("The best result is by " + bestResult +  ": ");
        for(int j = 0; j < RT[bestResult].length; j++) { 
            System.out.print(RT[bestResult][j]+" - "); 
           }

Will just print the wrong elements! For example I know the answer in the array should print:

11 - 10 - 0 - 0 - 0 ..etc

Instead it prints elements from previous rows/prints random non-sense:

4 - 0 - 11 - 10 - 0 - 0 - 0 ..etc 

Attempting to print it like this however:

System.out.println(Arrays.deepToString(MDarray));

Is evidence that data is being stored in my arrays correctly, as it prints results exactly correct every time.

So my question is: Is there an approach to printing and accessing results with Arrays.deepToString() or something similar? I mostly want to access the row of the winning result, trim off the excess 0's (which I'll figure out after solving this) and send the results to a web application for use. Thanks for any help guys.

How does one access just a specific row?

You can access the row of a 2 dimensional array by returning the element of the first dimension with the index of the desired row number.

Code example:

double[][] array = {{1,1,1},{2,2,2},{3,3,3}};

double[] winner = array[0]; 

winner will correspond to the first "row" of your array. The output of:

System.out.println(Arrays.toString(winner));

will then give you the result:

[1.0, 1.0, 1.0]

double[][] MDarray = new double[10][20]

So this will be 20 rows by 10 columns. With the way my program works, writing into these elements means that the final 10-15 elements of every row will likely be populated with just '0's, here is an example of what my MD array's would look like with populated elements:

[ [0, 1, 2, 3, 4, 5, 6, 0, 0, ..] [0, 1, 2, 3, 4, 5, 6, 7, 8, 0, ..] [0, 1, 2, 3, 4, 0, 0, ..] .. etc

I think you misunderstood how multidimensional arrays work.

The highlighted line of code that I'm referencing is actually creating 10 rows of 20 columns each. The way to access a concrete row of the multidimensional array is like you've done before:

System.out.println(cool_MDarray[myFavoriteRow].length);

Or accessing a member of it:

System.out.println("Element is: "+md_array[index_row][index_col].toString());

Still, if none of these were your problem I'd recommend you to check the way you're filling your MD array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM