以下是一个示例代码,将一个整型数组分成每5个一组进行处理:

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

int batchSize = 5;
int batchCount = array.length / batchSize;

for (int i = 0; i < batchCount; i++) {
    int startIndex = i * batchSize;
    int endIndex = startIndex + batchSize;

    int[] batchArray = Arrays.copyOfRange(array, startIndex, endIndex);

    // 对每个批次的数据进行处理
    for (int j = 0; j < batchSize; j++) {
        int value = batchArray[j];

        // 在这里进行处理
        // ...
    }
}

// 如果最后一组数据不足5个元素,单独处理
if (array.length % batchSize != 0) {
    int startIndex = batchCount * batchSize;
    int endIndex = array.length;

    int[] lastBatchArray = Arrays.copyOfRange(array, startIndex, endIndex);

    // 对最后一组数据进行处理
    for (int j = 0; j < lastBatchArray.length; j++) {
        int value = lastBatchArray[j];

        // 在这里进行处理
        // ...
    }
}

这个示例代码使用了 Arrays.copyOfRange 方法将原数组按照起始和结束下标复制成一个新数组,然后对新数组进行处理。每次处理完一个批次后,如果最后一组数据不足5个元素,就单独处理最后一组数据。

android java 一个整型数组有12个元素每5个一组分批处理

原文地址: https://www.cveoy.top/t/topic/bX4i 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录