开发者

Beginners Java Array Question

I'm trying to output a square of X's using an array. The diagonals of the square will be filled with 'X' and the empties will be filled with spaces '_'.

Here's the code I got:

public static char[][] square(int z) {
    int size=5;
    char[][] myArr = new char[size][size];
    for (int c=0;c<size;c++)
        myArr[c][c]='X';
    for (int r=0;r<size;r++)
    {
        for (int col=size-1;col>=0;col--)//put X
        {
            myArr[r][col]='X';
        }
    }
    for(int count=0;count<size;count++){
开发者_Go百科        if (myArr[count][count]!='X')
            myArr[count][count]=' ';
    }

    return myArr;

}

This should be working-I ran it manually on paper and everything should have been fine. What can the problem be?


The problem is probably, that here:

for (int r=0;r<size;r++)
{
    for (int col=size-1;col>=0;col--)//put X
    {
        myArr[r][col]='X';
    }
}

you are itering over the whole square (size * size) and not just drawing the Northeast - Southwest diagonal.

Try to replace it with this:

for (int r=0;r<size;r++)
{
    myArr[r][size - r] = 'X'
}

EDIT: To make your code little bit compacter:

public static char[][] square(int size) {
    char[][] myArr = new char[size][size];
    for (int c = 0; c < size; c++) {
        for (int r = 0; r < size; r++) {
            if ((c == r) || ( c == size - r)) {
                myArr[r][c] = 'X';
            } else { 
                myArr[r][c] = ' ';
            }
        }
    }
    return myArr;
}


When you Iterate your loop, you want the diagonals to be 'x'. You can save a lot of work and code by minimizing how much you iterate.

public static char[][] square(int z) {
int size = z;
char[][] myArr = new char[size][size];

for(int i = 0;i < size;i++)
{
    for(int j = 0;j < size;j++)
    {
        if(i == j)
        {
             myArr[i][j] = 'X';
        }
        else if(i + j == size - 1)
        {
             myArr[i][j] = 'X';
        else
        {
             myArr[i][j] = " ";
        }
     }
}

return myArr;

}


You code seems a little bit overcomplicated with all those ifs and clever loops.
Just fill array with 'background chars' and then draw diagonal.

    char[][] myArr = new char[size][size];
    for (int i = 0; i < size; ++i) {
        Arrays.fill(myArr[i], ' ');
    }
    // now we have square filled with spaces

    // draw diagonal, like you did it
    for (int c = 0; c < size; c++) {
        myArr[c][c] = 'X';
        myArr[c][size - c - 1] = 'X';
    }

edit
Updated to draw two diagonals.


First problem is that when you do new char[size][size]; you still should new each char array before you use it.

This can be merged with the first for loop easily:

 for (int c=0;c<size;c++) {
    myArr[c] = new char[size]; // allocate the array
    myArr[c][c]='X';
 }

Next the algorithm doesn't work, though this one would:

public static char[][] square (int z) {
    int size = z, cap=((size+1) /2);
    char[][] myArr = new char[size][size];
    for (int c = 0; c < cap; c++) { // iterate only half the array doing 4 positions per iteration
        myArr[c] = new char[size];
        myArr[size-c-1] = new char[size];
        Arrays.fill(myArr[size-c-1],' '); // make the new line blank
        Arrays.fill(myArr[c], ' '); // make the new line blank
        myArr[c][c] = 'X';  // top left to center
        myArr[size - c - 1][size-c-1] = 'X'; // bottom right to center
        myArr[size - c - 1][c] = 'X'; // bottom left to center
        myArr[c][size - c - 1] = 'X'; // top right to center
    }
    return myArr;
}

/** test square algorithm by printing it to System.out */
private static void testSquare () {
    char[][] sq = square(5);
    for (char[] l : sq) {
        System.out.println(new String(l));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜