2015-10-14 115 views
0

我试图产生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.

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) 
  • 获取零在数组中。不应该有零。
  • 输出一个具有如:

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

    回答

    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值以保持逻辑。

    下面是代码:

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

    谢谢你,工作就像一个魅力:) – hank99

    +0

    @ hank99不客气。请享用 :) –

    0

    您可以通过将一个System.out的声明只是数组声明之前模拟所有这些场景。

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

    1. 一些输出仅仅是[[]]或[]

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

  • n必须是正
  • 这是发生在RandMaxColums为零。 random.nextInt(n)的输入必须大于0,它会引发异常。

    1. 获取数组中的零。不应该有零。

    内部for-loop应该是这样的。

    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]]