You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中与Numpy的array_split等效的实现方法是什么?

Java中实现类似Numpy array_split的数组分割功能

要在Java中实现和Numpy array_split 相同的功能——将数组分割为n个大小大致相等的块(当数组总长度无法被n整除时,前若干个块会比后面的块多1个元素),可以通过手动实现逻辑完成,无需依赖第三方库也能灵活达成需求。

手动实现方案

泛型版本(支持所有对象类型数组)

该实现可以处理Integer[]、String[]等对象类型的数组,利用反射创建对应类型的子数组:

import java.util.ArrayList;
import java.util.List;

public class ArraySplit {
    public static <T> List<T[]> splitArray(T[] array, int n) {
        List<T[]> result = new ArrayList<>();
        int length = array.length;
        int baseSize = length / n;
        int remainder = length % n;
        
        int startIndex = 0;
        for (int i = 0; i < n; i++) {
            // 前remainder个块的大小为baseSize+1,其余为baseSize
            int currentChunkSize = baseSize + (i < remainder ? 1 : 0);
            // 通过反射创建对应类型的子数组
            @SuppressWarnings("unchecked")
            T[] chunk = (T[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), currentChunkSize);
            // 复制原数组中的对应元素到子数组
            System.arraycopy(array, startIndex, chunk, 0, currentChunkSize);
            result.add(chunk);
            startIndex += currentChunkSize;
        }
        return result;
    }

    public static void main(String[] args) {
        Integer[] inputArray = {7, 3, 9, 10, 5, 6, 8, 13};
        List<Integer[]> chunks = splitArray(inputArray, 3);
        // 输出分割结果
        for (Integer[] chunk : chunks) {
            System.out.print("[");
            for (int i = 0; i < chunk.length; i++) {
                if (i > 0) System.out.print(", ");
                System.out.print(chunk[i]);
            }
            System.out.println("]");
        }
    }
}

基本类型数组版本(以int数组为例)

对于int[]、long[]等基本类型数组,可以编写更高效的专用实现,避免反射操作:

import java.util.ArrayList;
import java.util.List;

public class BasicTypeArraySplit {
    public static List<int[]> splitIntArray(int[] array, int n) {
        List<int[]> result = new ArrayList<>();
        int length = array.length;
        int baseSize = length / n;
        int remainder = length % n;
        
        int startIndex = 0;
        for (int i = 0; i < n; i++) {
            int currentChunkSize = baseSize + (i < remainder ? 1 : 0);
            int[] chunk = new int[currentChunkSize];
            System.arraycopy(array, startIndex, chunk, 0, currentChunkSize);
            result.add(chunk);
            startIndex += currentChunkSize;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] inputArray = {7, 3, 9, 10, 5, 6, 8, 13};
        List<int[]> chunks = splitIntArray(inputArray, 3);
        for (int[] chunk : chunks) {
            System.out.print("[");
            for (int i = 0; i < chunk.length; i++) {
                if (i > 0) System.out.print(", ");
                System.out.print(chunk[i]);
            }
            System.out.println("]");
        }
    }
}

第三方库说明

常见的Java工具库(如Guava、Apache Commons Lang)没有直接提供按指定数量分割为大致相等块的方法,需要基于块大小计算手动实现,因此无依赖的手动实现反而更直接灵活。

内容的提问来源于stack exchange,提问作者TMOTTM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 15:45:50