随机打乱数组

版权声明: https://blog.csdn.net/Dongguabai/article/details/84755328

Java: 

package com.example.threaddesign;

/**
 * @author Dongguabai
 * @date 2018/12/2 20:58
 */
public class ThreadTest {

    public static void main(String[] args) {

        Integer[] arr = {1, 2, 3, 4, 5};
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            int end = 4;
            int index = (int) (Math.random() * (end + 1));
            int t = arr[end];
            arr[end] = arr[index];
            arr[index] = t;
        }

        for (Integer integer : arr) {
            System.out.println(integer);
        }
    }

}

JS:

猜你喜欢

转载自blog.csdn.net/Dongguabai/article/details/84755328
今日推荐