Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;

public class Main {
    /**
     * Splits an array of objects into an array of smaller arrays. Useful for multithreaded bots.
     * CAVEAT: if splits > z.length, splits will be set to z.length.
     * 
     * @param z The array we'll be splitting
     * @param splits The number of sub-arrays you want.
     * 
     */

    public static Object[][] arraySplitter(Object[] z, int splits) {

        if (splits > z.length)
            splits = z.length;

        Object[][] xf;

        if (splits == 0) {
            xf = new Object[][] { z };
            return xf;
        } else {
            xf = new Object[splits][];
            for (int i = 0; i < splits; i++) {
                Object[] temp;
                if (i == 0)
                    temp = Arrays.copyOfRange(z, 0, z.length / splits);
                else if (i == splits - 1)
                    temp = Arrays.copyOfRange(z, z.length / splits * (splits - 1), z.length);
                else
                    temp = Arrays.copyOfRange(z, z.length / splits * (i), z.length / splits * (i + 1));

                xf[i] = temp;
            }
            return xf;
        }
    }
}