简体   繁体   English

如何从二维数组(java)中随机获取一对元素

[英]How to get randomly a pair elements from a 2d Array (java)

I have the following Array:我有以下数组:

arr= new int[][] { { 20, 0 }, { 2, 1 }, { 3, 2 }, { 1, 0 } };

I want getting randomly one lement from arr .我想从arr中随机获得一个元素。 For example: [3,2] or [20,0]例如: [3,2][20,0]

I'm using the following code:我正在使用以下代码:

Random r=new Random();        
    int rd= rand.nextInt(arr.length);

You can use int[] randomPair = arr[randomNumber];您可以使用int[] randomPair = arr[randomNumber]; . . If you want to get that numbers in seperated variables, you can add int num1 = randomPair[0];int num2 = randomPair[1];如果您想在单独的变量中获取这些数字,您可以添加int num1 = randomPair[0];int num2 = randomPair[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM