如何将字符串数字随机插入数组-Java

时间:2018-04-10 13:02:59

标签: java android arrays random

我在这里有一个数组strings这是我的拼图板视图, 现在一切正常但数组是硬编码的,我需要从0-4随机生成字符串。 我试图获得random char并将其设为string,但这并不起作用。任何提示都会很好。

Random rand = new Random();
char c = (char)(rand.nextInt(5) + '0');
StringBuilder sb = new StringBuilder();
sb.append(c);

String[] debug_board_state  = new String[7];
debug_board_state[0] = "0,3,0,0,3,0,2";
debug_board_state[1] = "1,0,2,0,0,1,2";
debug_board_state[2] = "0,2,0,0,0,0,0";
debug_board_state[3] = "0,0,3,0,3,0,4";
debug_board_state[4] = "2,0,0,0,0,1,0";
debug_board_state[5] = "0,1,0,0,1,0,2";
debug_board_state[6] = "2,0,3,0,0,2,0"; 

更新。

感谢用户回答我能够获得随机矩阵,虽然我遇到了另一个问题,但我需要对矩阵做更多的事情,所以我不想打印出来。这是代码

static private final int WIDTH_EASY = 7;
 protected void InitializeEasy() {
      Random rand = new Random();

      String[][] debug_board_state  = new String[7][7];
      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              debug_board_state[row][column] = String.valueOf(rand.nextInt(5));
          }
      }

      for (int row = 0; row < debug_board_state.length; row++) {
          for (int column = 0; column < debug_board_state[row].length; column++) {
              System.out.print(debug_board_state[row][column] + " ");
          }
      };

    for (int i = 0; i < WIDTH_EASY; ++i) {
      StringTokenizer tokenizer = new StringTokenizer (debug_board_state[i][i], ",");
      int column = 0;
      while(tokenizer.hasMoreTokens()) {
    String token = tokenizer.nextToken();
    getCurrentState().board_elements[i][column] = new BoardElement();
    getCurrentState().board_elements[i][column].max_connecting_bridges = Integer.parseInt(token);
    getCurrentState().board_elements[i][column].row = i;
    getCurrentState().board_elements[i][column].col = column;

    if (getCurrentState().board_elements[i][column].max_connecting_bridges > 0) {
      getCurrentState().board_elements[i][column].is_island = true;
    }
    ++column;
      }
    }
  }

字符串Tokenizer适用于1d数组但不适用于2d,我需要一些与StringTokenizer相同的东西并将其应用于矩阵。我收到以下错误

java.lang.NullPointerException: Attempt to read from field Island_and_Bridges.Hashi.BoardElement[][] Island_and_Bridges.Hashi.BoardState$State.board_elements on a null object reference

4 个答案:

答案 0 :(得分:1)

虽然我认为int[][]是一个更好的主意,但这里是String[][]解决方案。您可以使用String.valueOf(rand.nextInt(5))在矩阵中生成元素:

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        Random rand = new Random();

        String[][] matrix  = new String[7][7];
        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[row].length; column++) {
                matrix[row][column] = String.valueOf(rand.nextInt(5));
            }
        }

        for (int row = 0; row < matrix.length; row++) {
            for (int column = 0; column < matrix[row].length; column++) {
                System.out.print(matrix[row][column] + " ");
            }
            System.out.println();
        }
    }
}

更新

for (int row = 0; row < WIDTH_EASY; ++row) {
    for (int column = 0; column < WIDTH_EASY; ++column) {
        getCurrentState().board_elements[row][column] = new BoardElement();
        getCurrentState().board_elements[row][column].max_connecting_bridges = Integer.parseInt(debug_board_state[row][column]);
        getCurrentState().board_elements[row][column].row = row;
        getCurrentState().board_elements[row][column].col = column;
        if (getCurrentState().board_elements[row][column].max_connecting_bridges > 0) {
            getCurrentState().board_elements[row][column].is_island = true;
        }
    }
}

答案 1 :(得分:0)

这样的东西?

伪代码:

String[][] debug_board_state  = new String[7][7];

for (int x = 0; x < debug_board_state.size(); x++) {
     for (int y = 0; y < debug_board_state[x].size(); y++) {
         debug_board_state[x][y] = new_random_character();
     }
}

答案 2 :(得分:0)

0-4位于ASCII范围内的49到52之间:

Random rand = new Random();
char c = (char)(rand.nextInt(4)+49);
StringBuilder sb = new StringBuilder();
sb.append(c+'0');

答案 3 :(得分:0)

也许,你想要这样的东西:

public void initBoard() {
    Random random = new Random();
    String[][] board = new String[7][7];
    for (int i=0; i < board.size(); i++) {
        for (int j=0; j < board[].size(); j++) {
            board[i][j] = String.valueOf(random.nextInt() % 5);
        }
    }
}

它将使用随机数String初始化您的电路板。