Java List:当我调用Arrays.fill()来填充数组时,为什么它会出现一些意想不到的元素?

Wel*_*len 3 java eclipse

我是初学者程序员,这是我第一次在这里提问,如果对格式有任何担心,我会感到非常抱歉.首先,感谢您的回答,感谢您的耐心和赦免.

实际上问题是"循环直到找到4种不同颜色的扑克牌".我使用数组来表示卡号,并使用数组来表示它们的颜色.并且我用for语句填充cardNo数组,值从1到4,但它显示为0,我不知道为什么,我认为这是问题发生的地方,这就是我的所有代码.

public static void collect() {
    int num=0;
    //represent 52 Poker Cards
    int [] cardNo=new int[52];
    List list=new ArrayList<>();
    // `int[] color=new int[4]` to represent 4 colors, so the value 
    // of cardNo[] elements is the Color.
    for(int i=0;i<4;i++) {
        Arrays.fill(cardNo, i*13, (i+1)*13-1, i+1);
    }
    for(int n:cardNo) {
        list.add(n);
    }
    Collections.shuffle(list);
    // I use this for statement to check if it's worry with Arrays.fill(),
    // or it's about the list.add().
    for(int i:cardNo) {
        System.out.print(i+" ");
    }
    System.out.println(list);

    /*int[] color=new int[4];
    while(color[0]==0||color[1]==0||color[2]==0||color[3]==0) {
        int n=(int)(Math.random()*53);
        color[(int) list.get(n)-1]++;
        num++;
    }
    System.out.println("Number of picks: "+num);*/
}
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 5

toIndex参数Arrays.fill是排他性的,因此产生变化:

Arrays.fill(cardNo, i*13, (i+1)*13 - 1, i+1);
Run Code Online (Sandbox Code Playgroud)

Arrays.fill(cardNo, i*13, (i+1)*13, i+1);
Run Code Online (Sandbox Code Playgroud)

Javadoc:

空隙java.util.Arrays.fill(INT []一,INT的fromIndex,INT元素范围为INT val)的

将指定的int值分配给指定的int数组的指定范围的每个元素.要填充的范围从索引fromIndex(包括)扩展到索引toIndex,exclusive.(如果fromIndex == toIndex,则要填充的范围为空.)