1

how ca i shuffle this array randomly ? basically i have 4 buttons and i want to change the text of buttons randomly. for example: button have black,red,yellow,green receptively and i need to change these texts randomly when i click any of button.

this is my code

final int[] name={ R.string.text1,R.string.text2,R.string.text3,R.string.text4};
List<Integer> shuffle = new ArrayList<Integer>(Arrays.asList(name));

for(int i = 0; i <shuffle.Count(); i++)
 b[i].Text = shuffle[i];
Asif
  • 11
  • 2

1 Answers1

1

You can use Collections.shuffle(List) method

final Integer[] name={ 1, 2, 3, 4};
List<Integer> shuffle = new ArrayList<Integer>(Arrays.asList(name));

for(int i = 0; i <shuffle.size(); i++) {
    Collections.shuffle(shuffle);
    for (int j = 0; j < shuffle.size(); j++) {
        System.out.print(shuffle.get(j) + " ");
    }
    System.out.println();
}

In this example the output is:

4 3 1 2 
2 1 4 3 
3 1 2 4 
3 1 2 4 
szimon
  • 662
  • 1
  • 6
  • 10