2016-03-07 110 views
-2

我剛開始使用這個程序編寫代碼,所以對我來說很容易。 :)所以我嘗試添加隨機變量到一個數組列表,但他們一直到0。我認爲這可能是因爲它可能沒有添加任何東西到列表中?如何在另一個類(下面的類)中調用add2array方法?隨機數發生器不工作,越界異常?

public class myClass 
{ 
    // instance variables 
    public Random r; 
    public Random t; 
    public Random o; 
    public int randomR; 
    public int randomT; 
    public int randomO; 
    public ArrayList <Integer> myArray; 

    /** 
    * Constructor for objects of class 
    */ 
    public myClass() 
    { 
    r = new Random(); 
    t = new Random(); 
    o = new Random(); 
    int randomR = (r.nextInt(10)); 
    int randomT = (t.nextInt(10)); 
    int randomO = (o.nextInt(10)); 
    myArray = new ArrayList<Integer>(); 
    } 
    public void add2Array() 
    { 
    myArray.add(randomR); //myArray[0] is first random number 
    myArray.add(randomT); //myArray[1] is second random number 
    myArray.add(randomO); //myArray[2] is third random number 
    } 
} 

然後這個代碼在另一個類中。我嘗試在類中調用printArray,並且它說有一個超出界限的異常。

public class myClass2 
{ 
    Arraylist<myClass> myArray; 


    public myClass2() 
    { 
    myArray = new ArrayList<myClass>(); 
    } 

    public void printArray() 
    { 
    System.out.println("Slot 1: " + myArray.get(0)); 
    System.out.println("Slot 2: " + myArray.get(1)); 
    System.out.println("Slot 3: " + myArray.get(2)); 
    } 
} 

然後我創建了一個「Main」類。仍然得到越界的錯誤。它指向的printArray方法是myClass2。

public static void main(String[] args) 
{ 
    myClass m = new myClass(); 
    myClass2 k = new myClass2(); 
    m.add2array(); 
    g.printArray(); 
} 

回答

4

在你的構造函數,你已經宣佈新的變量randomRrandomTrandomO被遮蔽的同名現有的實例變量。這些實例變量未分配,因此它們的默認值爲0。在你的構造函數,改變

int randomR = (r.nextInt(10)); 
int randomT = (t.nextInt(10)); 
int randomO = (o.nextInt(10)); 

randomR = (r.nextInt(10)); 
randomT = (t.nextInt(10)); 
randomO = (o.nextInt(10)); 
+0

this.randomR =(r.nextInt(10)); this.randomT =(t.nextInt(10)); this.randomO =(o.nextInt(10)); –

-1

如果你調用一個新的隨機()使用相同的種子,你會總是得到相同的答案。這裏有一種方式來獲得一個psuedorandom整數

using System.Security.Cryptography; 
. 
. 
. 

public int Randominteger(int minValue, int maxValue) 
{ 
     RNGCryptoServiceProvider crypt = new RNGCryptoServiceProvider(); 
     byte[] fourbytes = new byte[4]; 
     crypt.GetBytes(fourbytes); 
     return new Random(BitConverter.ToInt32(fourbytes, 0)).Next(minValue, maxValue); 
} 
+0

這是一個Jave解決方案,OP在標籤中指定了Java。 – zaph

1

我沒有看到你調用add2Array()方法的任何地方。你得到IndexOutOfBoundsException的原因是由於myArray沒有任何元素和執行代碼myArray.get(0)

確保改變whate @rgettman建議,否則在解決我提到的這個問題後你會得到0。

調用add2Array()myClass()構造

或在其他類

myClass m = new myClass(); 
m.add2Array(); 
m.printArray(); 

不知道你想實現的代碼是不是很好的事。另外在Java中,良好的約定是以大寫字母開頭的類名。

+0

如何在另一個類中調用add2array方法?我只是說add2array();在打印出數組列表之前? – LynnLove

+0

你還沒有提供其他的課程代碼。但是你必須做一些檢查答案。 –

+0

我創建了一個「Main」類,並添加了你的代碼,但是我仍然得到了越界異常...... public static void main(String [] args) { myClass m = new myClass(); myClass2 k = new myClass2(); m.add2array(); g.printArray(); } – LynnLove

0

使用Random的Java SecureRandom插件。

使用Java SecureRandom類和方法:nextInt(int n)它們在0(含)和指定值(不含)之間均勻分佈int值。

這是一個密碼安全的PRNG,不需要播種。

SecureRandomClass SecureRandomnextInt方法從Class Random繼承