Brandex07 Brandex07 - 4 months ago 10
Android Question

Array is not shuffling in android

I am trying to shuffle an array by converting it into a List, calling shuffle method, and then converting it back to the same array. However, the data is not shuffling for some reason. Is it because it is static? I have no errors, it just doesn't shuffle. Thanks.

public static String IMGS[] = {
"http://i.imgur.com/F6WgOZg.jpg",
"https://images.unsplash.com/photo-1439546743462-802cabef8e97?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1441155472722-d17942a2b76a?q=80&fm=jpg&w=1080&fit=max&s=80cb5dbcf01265bb81c5e8380e4f5cc1",
"https://images.unsplash.com/photo-1437651025703-2858c944e3eb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1431538510849-b719825bf08b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1434873740857-1bc5653afda8?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1439396087961-98bc12c21176?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1433616174899-f847df236857?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1438480478735-3234e63615bb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
"https://images.unsplash.com/photo-1438027316524-6078d503224b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

List<String> arr = Arrays.asList(IMGS);
Collections.shuffle(arr);
arr.toArray(IMGS);

Answer

I think you forgot to use the return of arr.toArray as an assignment to IMGS

    List<String> arr = Arrays.asList(IMGS);
    Collections.shuffle(arr);
    IMGS = arr.toArray(IMGS);