生成具有随机行长度的随机二维数组

Generate a random 2d array with random row length

我正在尝试生成类似于以下内容的二维随机数组:

 array[random][random2]
 { 
   {1, 2},
   {6, 4},
   {-1, 5},
   {-2}
 }

数组中的值也是随机的,可能有-9到-1到1到9个数。

这是我得到的:

public class gen2dArray {

   public static void main(String args[]) {

      Random random = new Random();
      int n = 0;
      int max = 5, min = 1;

      n = random.nextInt(max - min + 1) + min;

      int maxRowCol = (int)Math.pow(2,n);

      int RandMaxRows = random.nextInt(maxRowCol);
      int RandMaxColums = random.nextInt(maxRowCol);


      int [][] array = new int [RandMaxRows][RandMaxColums];

      for (int i = 0; i < array.length; i++) {
         for (int j = 0; j < random.nextInt(array[i].length)+1; j++) {
           array[i][j] = random.nextInt(9) + 1;
         }
      }
      System.out.println(Arrays.deepToString(array));
   }
}

输出 1:

 [
   [4, 4, 5, 0], 
   [2, 3, 0, 0]
 ]

输出 2:

[
   [5, 2, 1, 0, 0],
   [3, 4, 2, 0, 0],
   [3, 1, 5, 0, 0, 0], 
   [4, 3, 2, 0, 0]
 ]

问题很少,

  1. 有些输出只是 [[]] 或 []

2.

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
            at java.util.Random.nextInt(Random.java:300)
            at gen2dArray.main(gen2dArray.java:23)
  1. 获取数组中的零。不应有零。

输出一必须是这样的:

[
   [4, 4, 5],
   [2, 3]
]

您可以通过在数组声明之前放置一个 System.out 语句来模拟所有这些场景。

System.out.println("RandMaxRows: " + RandMaxRows + "\tRandMaxColums: " + RandMaxColums);

  1. Some outputs are just [[]] or []

当您将 RandMaxRows 设为零或行和列均为零时,就会发生这种情况。我们使用 Random.nextInt(n) 来声明数组的大小。该方法的范围是0n-1。所以你可以通过增加方法的结果来解决这个问题。

  1. n must be positive

RandMaxColums 为零时会发生这种情况。 random.nextInt(n) 的输入必须大于 0,它会抛出异常。

  1. Getting the zeros in the array. There should be no zeros.

内部for循环应该是这样的。

for (int j = 0; j < array[i].length; j++) {

您需要用它的长度迭代数组,因为不需要任何随机数生成。使用当前长度并用随机生成的数字填充数组。


这是您的问题的工作代码:

public class gen2dArray {

  public static void main(String args[]) {

    Random random = new Random();
    int n = 0;
    int max = 5, min = 1;

    n = random.nextInt(max - min + 1) + min;

    int maxRowCol = (int)Math.pow(2,n);

    int RandMaxRows = random.nextInt(maxRowCol);
    int RandMaxColums = random.nextInt(maxRowCol);

    System.out.println("RandMaxRows: " + RandMaxRows + "\tRandMaxColums: " + RandMaxColums);
    int [][] array = new int [RandMaxRows+1][RandMaxColums+1];

    for (int i = 0; i < array.length; i++) {
      for (int j = 0; j < array[i].length; j++) {
        int temp = random.nextInt(9);
        array[i][j] = temp + 1;
      }
    }
    System.out.println(Arrays.deepToString(array));
  }
}

示例输出:

RandMaxRows: 3  RandMaxColums: 3
[[1, 3, 3, 3], [2, 7, 6, 1], [5, 4, 4, 1], [6, 4, 6, 9]]

如果你不想看到零,你应该避免让你的数组变大:

  int [][] array = new int [RandMaxRows][];
  // Code
  array[value] = new int[random.nextInt(maxRowCol)];

此方法应该为您提供一个由不同大小的不同数组组成的数组,并且只打印出它们的实际输入值,而不是 0 默认值。

这是一个输出示例:

[
    [2, 4, 8, 7, 6, 6, 9], 
    [1, 3, 4, 2], 
    [1, 4, 4, 2, 7, 6, 8], 
    [9, 3, 6, 3, 7, 3], 
    [4, 5, 3, 2, 5, 2, 8]
]

修改后的代码如下:

int [][] array = new int [random.nextInt(maxRowCol)][];

      for (int i = 0; i < array.length; i++) {
         array[i] = new int[random.nextInt(maxRowCol)];
         for (int j = 0; j < array[i].length; j++) {
           array[i][j] = random.nextInt(9) + 1;
         }
      }

当打印出[]时,表示你的Random做了return0

解决此问题的一个简单方法是将 1 添加到相关值,并可能将 MAX 值从 1 减少,以保持逻辑继续。

这是代码:

int maxRowCol = (int)Math.pow(2,n) - 1;
// Some code
int[][] array = new int [RandMaxRows + 1][];