集册 Java实例教程 将长值数组拆分为长[]

将长值数组拆分为长[]

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

489
将长值数组拆分为长[]


import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;/* 来自 时 代 J a v a*/

import java.util.Random;


public class Main{

    public static void main(String[] argv){

        long[] offsetArray = new long[]{34,35,36,37,37,37,67,68,69};

        int numPerBlock = 42;

        boolean shuffled = true;

        System.out.println(java.util.Arrays.toString(splitLongs(offsetArray,numPerBlock,shuffled)));

    }

    public static Random random = new Random();

    public static List<long[]> splitLongs(long[] offsetArray,

            int numPerBlock, boolean shuffled) {

        List<long[]> result = new ArrayList<long[]>(offsetArray.length

                / numPerBlock + 1);

        long[] tmp = Arrays.copyOf(offsetArray, offsetArray.length);

        if (shuffled) {

            CollectionUtil.shuffle(tmp, 0, tmp.length);//from nowjava.com - 时  代  Java

        }

        int i = 0;

        for (; i < offsetArray.length / numPerBlock; i++) {

            result.add(Arrays.copyOfRange(tmp, i * numPerBlock, (i + 1)

                    * numPerBlock));

        }

        if ((i) * numPerBlock < tmp.length) {

            result.add(Arrays.copyOfRange(tmp, i * numPerBlock, tmp.length));

        }

        return result;

    }

    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;

        }


        return array;

    }

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

    
展开阅读全文