Why does Collections.shuffle() fail for my array?

前端 未结 4 1235
萌比男神i
萌比男神i 2020-12-05 10:42

Why does my code not work?

package generatingInitialPopulation;

import java.util.Arrays;
import java.util.Collections;

public class TestShuffle {
    publi         


        
4条回答
  •  臣服心动
    2020-12-05 11:10

    That doesn't work because the call to shuffle is operating on the List returned by Arrays.asList, not on the underlying array. Thus, when you iterate over the array to print out the values, nothing has changed. What you want to do is save a reference to the List returned by Arrays.asList, and then print out the values of that List (rather than the values of the array) after you shuffle it.

提交回复
热议问题