Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Richard Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Richard Hirner (bitfire web engineering) - initial API and implementation
 ******************************************************************************/

import java.lang.reflect.Array;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T[][] partition(T[] bigArray, int max) {
        int nItems = bigArray.length;
        int nPartArrays = (nItems + max - 1) / max;

        T[][] partArrays = (T[][]) Array.newInstance(bigArray.getClass().getComponentType(), nPartArrays, 0);

        // nItems is now the number of remaining items
        for (int i = 0; nItems > 0; i++) {
            int n = (nItems < max) ? nItems : max;
            partArrays[i] = (T[]) Array.newInstance(bigArray.getClass().getComponentType(), n);
            System.arraycopy(bigArray, i * max, partArrays[i], 0, n);

            nItems -= n;
        }

        return partArrays;
    }
}