2017-03-05 32 views
0

我有一個小問題處理數組。「連接」Arraylist到整數表

@Override 
public void run() { 
    TypedArray images = getResources().obtainTypedArray(R.array.images_primes); 

    List<Integer> indexes = Arrays.asList(1,2,3,4,5,6,7,8,9,10); 
    Collections.shuffle(indexes); 

    Log.d("MYAPP", "value: " + indexes); 

    int randomPrimeNumber = (int) (indexes()); 

    // setImageResource to the random chosenImageNumber 
    imageViewMeasurement.setImageResource(images.getResourceId(randomPrimeNumber, R.color.colorGreyMeasuerementScreen)); 
} 

它是做什麼的?

  1. 從資源文件(工作) - >(typedArray圖像)中獲取圖像數組。

  2. 列表索引=列表,只要「圖像」 - >洗牌列表,工作! - > D/MYAPP:值:[4,9,10,8,3,1,7,2,5,6]

3/4。將是:通過「索引」洗牌清單,從那裏調用「圖像」,setImageresource陣列上的數字。

問題: 關於點2:我可以創建列表索引依賴於images.length的「圖像」-array?

關注點3-4:我無法將兩個數組(圖像/混排索引)放在一起。它顯示關於無法將整數轉換爲int的錯誤,當我嘗試設置時

int randomPrimeNumber = (int) (indexes); 

例如。

是否有可能「混合」兩個陣列?

最佳和感謝, tigercode

回答

0

,如果你想生成比images數組的長度小的隨機數沒有理由創造indexes列表,只需使用Random

Random random = new Random(); 
int randomNumber = random.nextInt(images.length()); 

是的,TypedArray類中有length()方法。

+0

不幸的是,沒有。我需要通過列表並從列表中隨機地調用每個項目,而不是從列表中挑選出一個項目。

 First code was: TypedArray images = getResources().obtainTypedArray(R.array.images_primes); int chosenImageNumber = (int) (Math.random() * images.length()); 
但這產生例如。來自10個試驗的1,3,3,4,4,6,7,8,9,10,不是每個項目;-) – tigercode