在Java中从数组中选择随机字符串

在Java中从数组中选择随机字符串,java,android,xml,string,indexoutofboundsexception,Java,Android,Xml,String,Indexoutofboundsexception,使用一个数组中的十个随机数,我想从另一个数组中选取十个随机字符串,但我得到的是ArrayIndexOutOfBoundsException int[] RandomArray = new int[10]; // this is an array of random numbers between 0 and 21 String[] StringArray = new String[22]; //This array has 22 diffrent sentences and gets them

使用一个数组中的十个随机数,我想从另一个数组中选取十个随机字符串,但我得到的是
ArrayIndexOutOfBoundsException

int[] RandomArray = new int[10]; // this is an array of random numbers between 0 and 21

String[] StringArray = new String[22]; //This array has 22 diffrent sentences and gets them from the string.xml

String[] ResultStringArray = new String[10]; //This array shall have now the 10 randomly picked sentences 

for (int i = 0; i < ResultStringArray.length; i++) {
    ResultStringArray[i] = StringArray[ResultArray[i]];
}
int[]随机数组=新的int[10];//这是一个介于0和21之间的随机数数组
String[]StringArray=新字符串[22]//这个数组有22个不同的句子,它们来自string.xml
字符串[]resultstringaray=新字符串[10]//这个数组现在应该有10个随机挑选的句子
for(int i=0;i
提供的代码有什么问题?

请尝试以下代码:

int N=10;
int[] randomArray = new int[N]; // this is an array of random numbers between 0 and 21

String[] stringArray = new String[22]; //This array has 22 diffrent sentences and gets them from the string.xml

String[] resultStringArray = new String[N]; //This array shall have now the 10 randomly picked sentences 

for (int i = 0; i < resultStringArray.length; i++) {
    // Pick the stringArray index from randomArray and not from resultArray
    resultStringArray[i] = stringArray[randomArray[i]];
}
int N=10;
int[]随机数组=新的int[N];//这是一个介于0和21之间的随机数数组
String[]stringArray=新字符串[22]//这个数组有22个不同的句子,它们来自string.xml
字符串[]resultstringaray=新字符串[N]//这个数组现在应该有10个随机挑选的句子
for(int i=0;i
ResultArray的大小比StringArrayalso大(更像是一种约定),变量应该以小写字母开头。规范是使用大写的类名。您还没有显示
ResultArray
的声明。您似乎将
ResultArray
RandomArray
混为一谈。为什么不洗牌数组(或者更好地使用
ArrayList
(它比
String[]
)和
collections更灵活。洗牌(…)
)并首先使用N个(在您的例子中是10个)元素?