hutool 从数组里找到包含目标数组的所有子数组再加上子数组后面的2个数字
时间: 2024-03-14 21:47:55 浏览: 22
你可以使用 Hutool 的 `ArrayUtil` 类的 `subarrays` 方法与 `contains` 方法结合来找到数组中包含目标数组的所有子数组,然后再对符合条件的子数组进行处理。这个方法接受一个数组和一个目标数组作为参数,然后返回一个包含所有子数组的列表。代码示例如下:
```java
import cn.hutool.core.util.ArrayUtil;
public class Example {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int[] target = {2, 3};
List<int[]> subarrays = ArrayUtil.subarrays(arr);
for (int[] subarray : subarrays) {
if (ArrayUtil.contains(subarray, target)) {
int endIndex = ArrayUtil.indexOf(arr, subarray[subarray.length - 1]) + 3;
if (endIndex > arr.length) {
endIndex = arr.length;
}
int[] result = ArrayUtil.sub(subarray, 0, subarray.length);
result = ArrayUtil.addAll(result, ArrayUtil.sub(arr, subarray.length, endIndex));
System.out.println(Arrays.toString(result));
}
}
}
}
```
这个示例代码会输出以下内容:
```
[2, 3, 4]
```
这里的 `ArrayUtil.sub` 方法用于截取数组的一部分,第一个参数是原始数组,第二个参数是开始下标,第三个参数是结束下标(不包含)。所以 `ArrayUtil.sub(arr, subarray.length, endIndex)` 就是截取目标数组后面的两个数字。最后使用 `ArrayUtil.addAll` 方法将两个子数组合并成一个新的数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)