我是android新手。我正在做quize app.I有一个JSON数组文本file.how生成随机数,在android中使用JSON数组重复排除。请帮助我 谢谢你... 这是我的示例代码
public static JSONArray getQuesList()throws JSONException{
ArrayList<Integer> list = new ArrayList<Integer>(size);
for(i =size - 1; i >= 0; i--) {
//index = rnd.nextInt(list.size());
list.add(i);
}
Random rand = new Random();
while(list.size() > 0) {
index = rand.nextInt(list.size());
Object object = quesList.get(index);
quesList.put(index, quesList.get(i));
quesList.put(i, object);
Log.d("","Selected: "+list.remove(index));
}
return quesList;
答案 0 :(得分:0)
<强>被修改强>
全局变量
int[] quizarray = null;
添加两个功能
private void createQuizIndex() {
int[] array = new int[QuizFunActivity.getQuesList().length()];
quizarray = new int[QuizFunActivity.getQuesList().length()];
for(int i = 0 ; i < QuizFunActivity.getQuesList().length() ; i++){
array[i] = i;
}
Random random = new Random();
int m = 0;
for (int n = array.length ; n > 0; n--){
int r = random.nextInt(n);
quizarray[m++] = array[r];
array[r] = array[n-1];
}
}
private int getIndexNum(int quesIndex2) {
return quizarray[quesIndex2];
}
更改每个 showQuestion
showQuestion(getIndexNum(quesIndex),review);
在第80行调用 createQuizIndex(),如果“重新拍摄”意味着另一个随机测验,则调用第194行