how you number the 2d array??i created a 2d array now i want to number the rouws and colums...here is the code i have now

int[][] Board = new int[15][15];
for (int r = 0; r < Board.length; r++) {

    for (int c = 0; c < Board.length; c++) {
        System.out.print(" " + "_" + " ");
    }

    System.out.println();
}

So i want to number this array

Recommended Answers

All 13 Replies

Imagine your items in a matrix. Like, for a[3][3]:

[0,0][0,1][0,2]
[1,0][1,1][1,2]
[2,0][2,1][2,2]

Hope you get it.

Even though your code may be working, there is an unsafe part which should not be implied this way...

int [][] Board= new int [15][15];
for(int r=0;r<Board.length;r++) {
  for(int c=0;c<Board.length;c++) {  // <-- Dangerous!!!
  // should at least do for(int c=0; c<Board[r].length; c++) {
    System.out.print(" "+"_"+" ");
  }
  System.out.println();
}

The second loop should not expect to use that value of Board.length because there could be a problem when your 2D array does not have the same length for row and column. If you really want, you could set a variable for column length outside the loop because a 2D array would always have the same column length for every row. But using the row length instead of column length in the loop is unsafe.

By the way, please look at Skyelher post and try to understand what it should be displayed. If you still don't get it, you may need to look at how a matrix is displayed.

/*
+--           --+      +--                     --+
| a00  a01  a02 |      | a00  a01  a02  a03  a04 |
| a10  a11  a12 |      | a10  a11  a12  a13  a14 |
| a20  a21  a22 |      | a20  a21  a22  a23  a24 |
+--           --+      +--                     --+
     3x3                           3x5
    matrix                        matrix
*/

I THINK i get it but i want to number the rows and columns of the array around the border...do i need another loop??

You could get the number around the border using 2 nested loop still. The only different is that you have check for which row you are at. If you are at the first and last row, then go through another loop for every column. If it is not, then pick only the first and last column values.

    int[][] Board = new int[15][15];
    for(int j=0;j<15;j++){
   if(j<9)System.out.print(" ");
    System.out.print(""+j);
    }
    for (int r = 0; r < Board.length; r++) {

        System.out.print("\n"+r);
        for (int c = 0; c < Board[r].length; c++) {

            System.out.print(" "+"_" );

        }
        System.out.println();

Dis is wat i got so far

And what output do you want? By the way, you should not hi-jack other's thread...

how im hi jacking another thread???its ma thread...i want da column number on the top..da code i have now showing me da row number on the left hand side I WANT THE COLUMN NUMBER ON TOP OF MA ARRAY "_"

0 1 2 3 4
0 - - - - -
1 - - - - -
2 - - - - -
3 - - - - -
4 - - - - -
line dis and "-" in dese

Ohh blur here, sorry. :P Anyway, please use the code tag provided by the forum...

That display actually gives a much better idea what you are doing. You would need 1 loop of column and 2 nested loop then...

// From your code above
int[][] Board = new int[15][15];
for (int column=0; column<15; column++) {  // first loop for the 1st row
  // display the column value in whatever format you want
}

for (int row=0; row<15; row++) {  // second loop for the table & underscore
  for (int column=0; column<15; column++) {
    if (column>0) {
      // display the '_' here
    }
    else {
      // display 'column' value here
    }
  }
}
public static void main(String[] args) {

        int[][] Board = new int[15][15];
        for (int c = 0; c < 15; c++) {

            System.out.print(c);
        }
        for (int r = 0; r < Board.length; r++) {
            for (int c = 0; c < Board[r].length; c++) {
                if (c > 0) {
                    System.out.print("_" + " ");

                } else
                    System.out.print(r);

            }
            System.out.println();

        }

    }

What the hell..still not right..the first line coming out with the column number..

and also the "_" will not match up with the column number...can plzz give me the actual code..or something similar plzz..dis is really annoying me noww..ughhh

    int[][] Board = new int[15][15];
    for (int c = 0; c < 15; c++) {

        System.out.print(c);
    }
    for (int r = 0; r < Board.length; r++) {
        for (int c = 0; c < Board[r].length; c++) {
            if (c > 0) {
                System.out.print("_" + " ");
            } else
                System.out.print(r);


        }
        System.out.println();

    }

its still not doing what it suppose to do..can any1 plzz help me with dis prblm??i got the row number i want to get the column number on the top..

You need to come up with a display format. That's why the numbers are not aligned the way you want. It is what you need to think through how to add white spaces in the right places.

/*
Below is what it should look like from your array. The dash (-) represents a white space.
Now, you see what it should look like, you should try to display it the way it is.
One note, this would work only array size less than 100 for both dimensions.

- <- represent a white space

-0--1--2--3--4--5--6--7--8--9-10-11-12-13-14
-0--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-1--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-2--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-3--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-4--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-5--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-6--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-7--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-8--_--_--_--_--_--_--_--_--_--_--_--_--_--_
-9--_--_--_--_--_--_--_--_--_--_--_--_--_--_
10--_--_--_--_--_--_--_--_--_--_--_--_--_--_
11--_--_--_--_--_--_--_--_--_--_--_--_--_--_
12--_--_--_--_--_--_--_--_--_--_--_--_--_--_
13--_--_--_--_--_--_--_--_--_--_--_--_--_--_
14--_--_--_--_--_--_--_--_--_--_--_--_--_--_
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.