生成随机行长度的随机2d数组

时间:2015-10-14 23:02:37

标签: java random multidimensional-array 2d

我正在尝试生成类似于:

的2d随机数组
 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. 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. 在数组中获取零。应该没有零。
    2. 输出一个必须像:

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

2 个答案:

答案 0 :(得分:1)

如果您不想看到零,则应避免使阵列变大:

  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确实返回0

解决此问题的一种简单方法是将1添加到相关值中,并可能将MAX值从1减小以保持逻辑运行。

以下是代码:

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

答案 1 :(得分:0)

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

System.out.println("RandMaxRows: " + RandMaxRows + "\tRandMaxColums: " + RandMaxColums);
  
      
  1. 有些输出只是[[]]或[]
  2.   

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

  
      
  1. n必须是正面的
  2.   

RandMaxColums为零时会发生这种情况。 <{1}}的输入必须大于random.nextInt(n),它会引发异常。

  
      
  1. 在数组中获取零。应该没有零。
  2.   

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

0

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

以下是您的问题的工作代码:

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));
  }
}