2012-03-02 119 views
0

有沒有辦法從一個對象數組中選取一個隨機對象?java隨機選擇一個類/對象

我嘗試了幾種我能想到但沒有用的方法。
我想在隨機類中使用一個函數(所有類將具有相同的功能但返回不同)。

我應該提到我試圖返回一個圖片,每一次我試圖返回一個隨機圖像它不返回點兒(「空」)

下面是我的一些代碼,我有問題用:

Car car; 
    Ford ford; 
    Mazda mazda 
    Fiat fiat 
    Rover rover 

    Car carlist[] = {ford,fiat,mazda,rover} 

public void paint(){ 
//this displays an image every 128 pixles 
     for (int i = 0;i<Width;i+=128){ 

      for(int j=128; j<Height;j+=128){ 
// this draws the image (the image is declaired in each car's class as getImage) 
       g.drawImage((car.carList[rand.nextInt(5)]).getImage(), i, j , this); 
        } 
       } 

代碼工作,如果我在把一個對象(而不是carcarList [rand.nextInt(5)]))。 每個cartypes延長汽車。

+1

您嘗試了哪些方法?發表你的代碼,你有問題 – Deco 2012-03-02 13:49:36

+0

在'0'和'length - 1'之間生成一個隨機整數(其中'length'是你數組的長度),並用它作爲索引來訪問數組以檢索你的對象。 – 2012-03-02 13:51:00

+0

我會發布代碼,但目前在移動設備上,並沒有它方便對不起 – user1159424 2012-03-02 13:55:26

回答

2

假設yourList是你的對象的陣列,使用該單條線:

Object randomObj = yourList[(int)Math.random() * list.length]; 
+4

不必要的乘法和從double到int的轉換。改用'Random'類。 – m0skit0 2012-03-02 13:57:01

+0

@aioobe Math.random不返回int – Teovald 2014-11-24 18:22:38

0

你可以讓他們都實現相同的接口,然後隨機選擇類。 這一些非常選中僞代碼:

public class myclass1 implements RandomInterface {} 


public class myclass2 implements RandomInterface {} 

public interface RandomInterface { 
    public void doStuff1(); 
    pubilc String doStuff2(); 
} 

    public class random{ 
     RandomInterface randomObj = yourList[(int)Math.random() * list.length]; 
     randomObj.doStuff1(); 
     randomObj.doStuff2(); 

    } 
+3

哇,太複雜了這麼簡單的問題。 – m0skit0 2012-03-02 13:53:47

+0

因爲他/她想要調用隨機類的方法,我只能看到這是方法還是使用instanceof解決方案? – Marthin 2012-03-02 13:55:22

+0

哦,我認爲你誤解了......「他/她想要隨機調用一個方法」這個他/她意味着java.util.Random類:)我刪除downvote;) – m0skit0 2012-03-02 14:00:03

1

您可以通過使用Random class解決這個問題是這樣的。

String rndStr = yourArray[new Random().nextInt(yourArray.length)]; 

如果你需要重複做,你應該考慮一個Random作爲成員變量和重用同一個實例爲您來電nextInt

1

您可以使用java.util.Random的方法nextInt()生成一個隨機索引到數組中。該方法需要一個參數指定返回的maximum - 1值:指定所述陣列的length

// Member variables. 
Object[] arr = ...; 
Random r = new Random(); 

Object nextRandmonObject() 
{ 
    return arr[r.nextInt(arr.length)]; 
} 
0

很容易:

Object your_array[]; 
[...] 
Random rnd = new Random(); 
Object random_element = your_array[rnd.nextInt(your_array.length)]; 
0

這樣

String[] a = new String[]{"1","2","3","4"}; 
    Random rand = new Random(); 
    String arbit = a[rand.nextInt(a.length)]; 
0

所有對象有一個返回的東西的功能,所以讓他們實現這個接口:

public interface ObjectReturningSomething 
{ 
    Object theFunction(); 
} 

那麼你的對象將是這樣的班,與功能恢復不同類型的對象:

public class ObjectReturningString implements ObjectReturningSomething 
{ 
    String theFunction() { return "Foo"; } 
} 
public class ObjectReturningInteger implements ObjectReturningSomething 
{ 
    Integer theFunction() { return 42; } 
} 

這裏是這樣的對象的數組:

ObjectReturningSomething[] objectArray = new ObjectReturningSomething[] { new ObjectReturningString(), new ObjectReturningInteger() }; 

這裏s如何隨機選擇一個:

int randomIndex = new Random().nextInt(array.length); 
ObjectReturningSomething randomObject = objectArray.get(randomIndex); 

這裏是如何調用該函數。

System.out.println(randomObject.theFunction()); 
+0

過度複雜。它的'int randomIndex = new Random()。nextInt(array.length);' – m0skit0 2012-03-03 11:53:10

+0

'nextInt(array.length)'確實更好,改進了。你認爲什麼部分「過於複雜」?你的答案根本不會完全回答問題(它回答瞭如何從列表中選擇一個隨機對象,但不知道如何調用該對象上的函數,這是問題的一部分)。 – 2012-03-03 12:32:23