用java语言用唯一随机数填充二维数组

用java语言用唯一随机数填充二维数组,java,arrays,Java,Arrays,有没有办法用唯一的随机数填充二维数组?我试了很多次,但都失败了。 我能做到 ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i < 26; i++) { //element will be in range (1,25) list.add(i); } Collections.shuffle

有没有办法用唯一的随机数填充二维数组?我试了很多次,但都失败了。 我能做到

ArrayList<Integer> list = new ArrayList<>();
        
        for (int i = 1; i < 26; i++) {         //element will be in range (1,25)
            list.add(i);
        }
        Collections.shuffle(list);
        for (int j = 0; j < 5; j++) {
            System.out.print(list.get(j) + "     ");
        }
        System.out.println();

我想您可以使用生成随机数和哈希集的库的组合。Hashset以记住迄今为止生成的随机数,如果生成了replicate,则重新生成,直到它给出了未知数为止

我想您可以使用库的组合来生成随机数和Hashset。Hashset可记住迄今为止生成的随机数,如果生成了duplicate,则重新生成,直到它给出了看不见的数字

这有帮助吗

public static void main(String[] args)
   {
      // declare arrays
      int[][] ticketInfo;
      String[][] seatingChart;

      // create arrays
      ticketInfo = new int [2][3];
      seatingChart =  new String [3][2];

      // initialize the array elements
      ticketInfo[0][0] = 15;
      ticketInfo[0][1] = 10;
      ticketInfo[0][2] = 15;
      ticketInfo[1][0] = 25;
      ticketInfo[1][1] = 20;
      ticketInfo[1][2] = 25;
      seatingChart[0][0] = "Jamal";
      seatingChart[0][1] = "Maria";
      seatingChart[1][0] = "Jacob";
      seatingChart[1][1] = "Suzy";
      seatingChart[2][0] = "Emma";
      seatingChart[2][1] = "Luke";

      // print the contents
      System.out.println(ticketInfo);
      System.out.println(seatingChart);
   }
这有帮助吗

public static void main(String[] args)
   {
      // declare arrays
      int[][] ticketInfo;
      String[][] seatingChart;

      // create arrays
      ticketInfo = new int [2][3];
      seatingChart =  new String [3][2];

      // initialize the array elements
      ticketInfo[0][0] = 15;
      ticketInfo[0][1] = 10;
      ticketInfo[0][2] = 15;
      ticketInfo[1][0] = 25;
      ticketInfo[1][1] = 20;
      ticketInfo[1][2] = 25;
      seatingChart[0][0] = "Jamal";
      seatingChart[0][1] = "Maria";
      seatingChart[1][0] = "Jacob";
      seatingChart[1][1] = "Suzy";
      seatingChart[2][0] = "Emma";
      seatingChart[2][1] = "Luke";

      // print the contents
      System.out.println(ticketInfo);
      System.out.println(seatingChart);
   }

如果要打印列表中的5x5矩阵,只需两层for循环即可。请参阅下面的代码


如果要打印列表中的5x5矩阵,只需两层for循环即可。请参阅下面的代码

试试这个

static List<List<Integer>> uniqueRandomNumbers(int height, int width) {
    List<Integer> list = IntStream.rangeClosed(1, height * width)
        .boxed()
        .collect(Collectors.toList());
    Collections.shuffle(list);
    List<List<Integer>> matrix = IntStream.range(0, height)
        .mapToObj(i -> list.subList(i * width, (i + 1) * width))
        .collect(Collectors.toList());
    return matrix;
}
试试这个

static List<List<Integer>> uniqueRandomNumbers(int height, int width) {
    List<Integer> list = IntStream.rangeClosed(1, height * width)
        .boxed()
        .collect(Collectors.toList());
    Collections.shuffle(list);
    List<List<Integer>> matrix = IntStream.range(0, height)
        .mapToObj(i -> list.subList(i * width, (i + 1) * width))
        .collect(Collectors.toList());
    return matrix;
}

它不起作用,因为ticketInfo和seatingChart是数组,在打印时,您将它们称为变量,如果我们在循环中调用它们,它将打印我们为它们设置的值。它不起作用,因为ticketInfo和seatingChart是数组,在打印时,您将它们称为变量,如果我们在循环中调用它们,它将打印我们为他们设定了
List<List<Integer>> matrix = uniqueRandomNumbers(5, 5);
for (List<Integer> list : matrix)
    System.out.println(list);
[16, 4, 15, 14, 25]
[19, 11, 6, 21, 9]
[17, 20, 3, 1, 5]
[10, 7, 22, 18, 2]
[12, 13, 24, 23, 8]