如何使用Java将一个数组按照指定元素拆分成多个新数组?

java 数组如何按指定元素拆分

给定一个数组,如何将其按照特定元素(例如 85)拆分为多个新数组?

public void mymethod(int[] arr, int splitvalue) {
    list> result = new arraylist<>();
    list temp = new arraylist<>();

    for (int num : arr) {
        if (num == splitvalue) {
            if (!temp.isempty()) {
                result.add(new arraylist<>(temp));
                temp.clear();
            }
            temp.add(num);
    

} else { temp.add(num); } } result.add(temp); for (list subarray : result) { system.out.println(arrays.tostring(subarray.toarray())); } }

使用该方法:

int[] arr = {85, -86, 13, 2, 99, 99, 99, 99, 98, 98, 99, 99, 99, 99, 20, 85, -86, 13, 2, 99, 99, 99, 99, 99, 99, 99, 85, 12, 85, -86, 13, 2, 99};
mymethod(arr, 85);

输出:

[85, -86, 13, 2, 99, 99, 99, 99, 98, 98, 99, 99, 99, 99, 20]
[85, -86, 13, 2, 99, 99, 99, 99, 99, 99, 99]
[85, 12]
[85, -86, 13, 2, 99]