Home > Blockchain >  java.util.Arrays Method
java.util.Arrays Method

Time:04-14

I am trying to create a grid matrix using a 2D array. The matrix should be mxm, this will vary on the user's input. The first column must consist of . and the columns thereafter must consist of *. I have imported the java.util.Arrays method and am using the toString function. The problem is that I am getting an output that looks like this:

[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]
[., *, *, *, *, *, *, *]

However, I want the code to look like this:

.*******
.*******
.*******
.*******
.*******
.*******
.*******
.*******

So essentially I want to know if there is a way to get the matrix to have no square brackets or commas and how would I go about achieving this?

import java.util.Arrays;

public class trial1 {
 
 public static void main(String[] args) {
      StdOut.println("What size matrix would you like?");
      int m = StdIn.readInt();
     
     // create board
     String[][] gameBoard = new String[m][m];
     
     for(int i = 0; i < gameBoard.length; i  ) {
         Arrays.fill(gameBoard[i], "*");
         gameBoard[i][0] = ".";
         StdOut.println(Arrays.toString(gameBoard[i]));
      }
        }

 }

CodePudding user response:

One change seem to give you the results you want:

You can use String.join

Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter

public static void main(String[] args) {
      int m = 5;
     
     // create board
     String[][] gameBoard = new String[m][m];
     
     for(int i = 0; i < gameBoard.length; i  ) {
         Arrays.fill(gameBoard[i], "*");
         gameBoard[i][0] = ".";
         System.out.println(String.join("", gameBoard[i]));
      }
    }

CodePudding user response:

as you see, the method of Arrays.toString() use "[" as prefix, "," as delimeter and "]" as suffix. the easiest way to archieve what u want is to provide a custom toString method for your case:

public static void main(String[] args) {
    System.out.println("What size matrix would you like?");
    int m = 7; // just assume 7

    // create board
    String[][] gameBoard = new String[m][m];

    for (int i = 0; i < gameBoard.length; i  ) {
        Arrays.fill(gameBoard[i], "*");
        gameBoard[i][0] = ".";
        System.out.println(toString(gameBoard[i]));
    }
}

private static String toString(String[] strings) {
    StringBuffer result = new StringBuffer();
    for (String string : strings) {
        result.append(string);
    }
    return result.toString();
}

CodePudding user response:

The simple solution is just to loop through all the elements one by one, and print them out.

So, for your loop, you would do:

for (int i = 0; i < gameBoard.length; i  ) {
     Arrays.fill(gameBoard[i], "*");
     gameBoard[i][0] = ".";

     //edited part
     for (int j = 0; j < gameBoard[0].length; j  ){
          System.out.print(gameBoard[i][j]);
     }

     System.out.println();
}

And note: this is similar to icodetea's solution, but a little bit easier to understand.

  • Related