Android shuffle Questions and Answers (string arrays) -
i making app in there list of questions , respective answers. questions in 1 string array, while answers in string array.
i have implemented following in wish shuffle questions. (of course answers need linked question, else meaningless)
code:
selected_q = new string[totalnoofq]; selected_a = new string[totalnoofq]; int[] random_code = new int[totalnoofq]; (int = 0; < totalnoofq; i++) { random_code[i] = i; } collections.shuffle(arrays.aslist(random_code)); (int j = 0; j < totalnoofq; j++) { int k = random_code[j]; selected_q [j] = databank_q [k]; selected_a[j] = databank_a [k]; }
the code reports no fatal error, selected_q still in sequential order. why? please show me how can amend codes? thanks!!!
you shuffle list created using random_code, random_code not modified.
you need create temporary list based on random_code. shuffle list , use fill selected_x arrays.
something should work :
int[] random_code = new int[totalnoofq]; (int = 0 ; < totalnoofq ; i++) { random_code[i] = i; } list<integer> random_code_list = new arraylist<integer>(); // create arraylist (arraylist used here because has indexes) (int idx = 0 ; idx < random_code.length ; idx++) { random_code_list.add(random_code[idx]); // fill } collections.shuffle(random_code_list); // shuffle (int j = 0 ; j < totalnoofq ; j++) { int k = random_code_list.get(j); // value selected_q[j] = databank_q[k]; selected_a[j] = databank_a[k]; }
Comments
Post a Comment