集册 Java实例教程 洗牌int数组

洗牌int数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

493
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
洗牌int数组


//package com.nowjava;


import java.util.Random;
/*
 from NowJava.com - 时  代  Java 
*/


public class Main {

    public static void main(String[] argv) {

        int[] array = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };

        System.out.println(java.util.Arrays.toString(shuffle(array)));

    }


    public static Random random = new Random();


    public static int[] shuffle(int[] array) {


        for (int i = 0; i < array.length; i++) {

            int randomPosition = random.nextInt(array.length);

            int temp = array[i];

            array[i] = array[randomPosition];

            array[randomPosition] = temp;

        }//n o w j a   v  a . c o m - 时  代  Java 提供


        return array;

    }


    public static void shuffle(long[] array, int startIndex, int endIndex) {

        assert (endIndex <= array.length && startIndex >= 0 && endIndex > 0);

        for (int i = startIndex; i <
展开阅读全文