From 6856a854f019141324c9b8377687849609d5e718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=8C=E7=9B=9B?= <2398716604@qq.com> Date: Thu, 13 Oct 2022 18:17:03 +0800 Subject: [PATCH] test --- .../cn/hutool/core/util/CollectionTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 hutool-core/src/test/java/cn/hutool/core/util/CollectionTest.java diff --git a/hutool-core/src/test/java/cn/hutool/core/util/CollectionTest.java b/hutool-core/src/test/java/cn/hutool/core/util/CollectionTest.java new file mode 100644 index 000000000..c80964953 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/util/CollectionTest.java @@ -0,0 +1,52 @@ +package cn.hutool.core.util; + +import org.junit.Assert; +import org.junit.Test; +import java.util.*; + +/** + * @author martin + * @create 2022-10-09 16:25 + */ +public class CollectionTest { + @Test + public void test() { + List list=new ArrayList<>(); + list.add(3); + list.add(5); + list.add(1); + list.add(2); + list.add(7); + list.add(4); + //排序工具 + Collections.sort(list); + //返回断开位置,如果返回size-1证明未断开 + int maxLength = findMaxLength(list); + Assert.assertEquals(1,maxLength); + + } + public int findMaxLength(List nums) { + //集合长度 + int n = nums.size(); + //遍历 + for (int i = 0; i < n; i++) { + //下一指针 + int next=i+1; + if (next>=n){ + break; + } + //当前值 + Integer num = nums.get(i); + //下一值 + Integer nextNum = nums.get(next); + if (num == null||nextNum==null){ + return i; + } + if (nextNum-num!=1){ + return i; + } + + } + return n-1; + } +}