2011-03-08 70 views
1

我期待初始化一個多維數組,其中包含0到100(含)之間的隨機數。我能夠在每個字段創建具有空值的多維數組,然後可以將100個隨機數添加到可用位置。Java初始化具有隨機整數的多維數組

但是,我想知道是否有一種方法來初始化隨機自然整數的多維數組。例如,我期待這樣做:

double[][] array = new double[][] {{0, 1, 2} , {1, 0, 3} , {2, 3, 0}}; 

這可能嗎?我在想:

double[][] array = new double[][] {{random values go here},{random values go here}}; 

有什麼建議嗎?

謝謝大家花時間閱讀。

米克

+0

您是否希望只是一次產生的隨機數,還是應在每次運行一組新的隨機數? –

+0

嗨凱文,我期待在每次運行中生成一組新的隨機數。 – MusTheDataGuy

回答

3

創建數組,然後在for循環中初始化它。 Random.nextInt(n)給你你所需要的。

下面是一個帶有三個不同運行的示例代碼。

import java.util.*; 
class Init { 
    public static void main(String ... args) { 

     Random random = new Random(); 

     double[][] array = new double[10][10]; 

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

     for(double[] a : array) { 
      System.out.println(Arrays.toString(a)); 
     } 
    } 
} 

輸出:

C:\Users\oreyes\langs\java>java Init 
[2.0, 92.0, 31.0, 98.0, 3.0, 5.0, 57.0, 41.0, 29.0, 89.0] 
[54.0, 57.0, 68.0, 92.0, 11.0, 20.0, 14.0, 58.0, 84.0, 23.0] 
[48.0, 14.0, 9.0, 33.0, 9.0, 27.0, 74.0, 34.0, 85.0, 91.0] 
[51.0, 87.0, 2.0, 96.0, 52.0, 81.0, 91.0, 95.0, 19.0, 56.0] 
[15.0, 90.0, 9.0, 85.0, 51.0, 23.0, 35.0, 21.0, 78.0, 14.0] 
[23.0, 20.0, 57.0, 94.0, 69.0, 99.0, 90.0, 78.0, 61.0, 38.0] 
[35.0, 61.0, 81.0, 72.0, 3.0, 93.0, 20.0, 96.0, 9.0, 35.0] 
[90.0, 100.0, 98.0, 14.0, 95.0, 75.0, 96.0, 8.0, 87.0, 25.0] 
[14.0, 41.0, 27.0, 57.0, 32.0, 37.0, 69.0, 61.0, 5.0, 42.0] 
[57.0, 0.0, 85.0, 28.0, 78.0, 47.0, 89.0, 54.0, 50.0, 59.0] 

C:\Users\oreyes\langs\java>java Init 
[3.0, 27.0, 37.0, 31.0, 52.0, 19.0, 63.0, 81.0, 88.0, 12.0] 
[80.0, 27.0, 7.0, 55.0, 21.0, 100.0, 73.0, 62.0, 9.0, 91.0] 
[85.0, 50.0, 66.0, 27.0, 63.0, 44.0, 0.0, 37.0, 93.0, 82.0] 
[73.0, 57.0, 4.0, 80.0, 5.0, 51.0, 63.0, 13.0, 97.0, 11.0] 
[87.0, 62.0, 20.0, 14.0, 44.0, 77.0, 71.0, 42.0, 27.0, 82.0] 
[37.0, 32.0, 96.0, 95.0, 45.0, 8.0, 11.0, 38.0, 61.0, 6.0] 
[34.0, 67.0, 84.0, 50.0, 38.0, 64.0, 50.0, 51.0, 50.0, 47.0] 
[79.0, 31.0, 54.0, 37.0, 27.0, 54.0, 57.0, 30.0, 77.0, 36.0] 
[74.0, 20.0, 98.0, 37.0, 8.0, 17.0, 18.0, 1.0, 29.0, 56.0] 
[21.0, 4.0, 33.0, 87.0, 4.0, 76.0, 65.0, 62.0, 76.0, 96.0] 

C:\Users\oreyes\langs\java>java Init 
[17.0, 3.0, 78.0, 32.0, 99.0, 76.0, 94.0, 93.0, 31.0, 55.0] 
[4.0, 25.0, 63.0, 68.0, 58.0, 39.0, 7.0, 55.0, 73.0, 86.0] 
[96.0, 89.0, 6.0, 100.0, 20.0, 58.0, 100.0, 91.0, 35.0, 46.0] 
[3.0, 16.0, 88.0, 82.0, 85.0, 35.0, 0.0, 1.0, 91.0, 78.0] 
[3.0, 33.0, 77.0, 10.0, 69.0, 60.0, 75.0, 58.0, 8.0, 31.0] 
[72.0, 36.0, 2.0, 19.0, 39.0, 15.0, 5.0, 74.0, 16.0, 28.0] 
[48.0, 71.0, 38.0, 17.0, 37.0, 34.0, 80.0, 98.0, 16.0, 42.0] 
[66.0, 74.0, 96.0, 80.0, 75.0, 7.0, 14.0, 46.0, 63.0, 56.0] 
[4.0, 15.0, 8.0, 93.0, 58.0, 21.0, 81.0, 100.0, 2.0, 44.0] 
[20.0, 71.0, 41.0, 43.0, 83.0, 7.0, 60.0, 28.0, 99.0, 42.0] 
+0

非常感謝你這個美好的回答。不幸的是,我嘗試這個時遇到錯誤。該錯誤標記在下面的行上:array [i] [j] = random.nextInt(101);它突出顯示/下劃線'nextInt',並顯示以下消息:「方法nextInt(int)未定義爲Integer類型」。這個問題會在這裏發生什麼?謝謝,OscarRyz。 – MusTheDataGuy

+0

你已經將隨機定義爲'Integer',並將其聲明爲'Random',如下所示:'Random random = new Random()' – OscarRyz

+0

我已經嘗試過了,仍然沒有運氣,不幸的是...我的其他地方可能會出現一個錯誤eclipse沒有檢測到的項目? – MusTheDataGuy

1

Random.nextInt()你想要做什麼?

+1

Random.nextInt(101)將會訣竅。 –

+0

謝謝你們,但不幸的是這並沒有奏效。我可以問我如何將Random.nextInt()/ Random.nextInt(101)方法實現到我的3D數組中?謝謝。 – MusTheDataGuy