Sunday 15 July 2012

Android ArrayList Shuffle -



Android ArrayList Shuffle -

i have arraylist> store questioners here php {{question, opta, optb, optc, optd, answer},{question, opta, optb, optc, optd, answer}, .....}}

how shuffle a,b,c,d d,b,a,c? {question, optd, optb, opta, optc, answer}

im using code

//myarray arraylist<arraylist<string>> getquestion = new arraylist<arraylist<string>>(); arraylist<string> choices; //first retrive elements getquestionarray questindex = getquestion.get(arrayrow).get(0); aindex = getquestion.get(arrayrow).get(1); bindex = getquestion.get(arrayrow).get(2); cindex = getquestion.get(arrayrow).get(3); dindex = getquestion.get(arrayrow).get(4); answerindex = getquestion.get(arrayrow).get(5); //get right reply (this word of senteces) string correctanswer = ""; if(answerindex.equals("a")){ correctanswer = aindex; }else if(answerindex.equals("b")){ correctanswer = bindex; }else if(answerindex.equals("c")){ correctanswer = cindex; }else if(answerindex.equals("d")){ correctanswer = dindex; } //im using shuffle abcd im creating new arraylist choices.add(aindex); choices.add(bindex); choices.add(cindex); choices.add(dindex); collections.shuffle(choices); int correctindex; //find our right reply correctindex = choices.indexof(correctanswer); //our right letter switch(correctindex){ case 0: answerindex = "a"; break; case 1: answerindex = "b"; break; case 2: answerindex = "c"; break; case 3: answerindex = "d"; break; } aindex = choices.get(0); bindex = choices.get(1); cindex = choices.get(2); dindex = choices.get(3); question.settext(questindex); ca.settext(aindex); cb.settext(bindex); cc.settext(cindex); cd.settext(dindex);

is there way that?

i create class myquestion fields:

public class myquestion{ string question; string answer; list<string> options; }

and phone call collections.shuffle(options);

or create method this:

public static void shuffle(list list, int a, int b){ list shufflelist = new arraylist(); (int = a; <= b; i++) shufflelist.add(list.get(i)); collections.shuffle(shufflelist); (int = a; i<= b; i++) list.set(i, shufflelist.get(i-a)); }

i didn't utilize generics faster, it's easy add together them

android

No comments:

Post a Comment