[英]How to get randomly a pair elements from a 2d Array (java)
我有以下數組:
arr= new int[][] { { 20, 0 }, { 2, 1 }, { 3, 2 }, { 1, 0 } };
我想從arr
中隨機獲得一個元素。 例如: [3,2]
或[20,0]
我正在使用以下代碼:
Random r=new Random();
int rd= rand.nextInt(arr.length);
您可以使用int[] randomPair = arr[randomNumber];
. 如果您想在單獨的變量中獲取這些數字,您可以添加int num1 = randomPair[0];int num2 = randomPair[1];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.