需求如图所示,伪代码如下(返回List元素相同且相邻元素的起始下标,没有相同的就返回他的下标)![](https://i-blog.csdnimg.cn/blog_migrate/519f9c3cd4fa607a34e8004019a17d10.png)
public static void main(String[] args) {
List<String> list = Lists.newArrayList();
list.add("日照");
list.add("日照");
list.add("日照");
list.add("威海");
list.add("日照");
list.add("日照");
list.add("日照");
list.add("青岛");
list.add("青岛");
list.add("威海");
list.add("威海");
list.add("威海");
start(list);
}
private static void start(List<String> list) {
for (int i = 0; i < list.size(); ) {
for (int t = i; t < list.size(); ) {
if (list.get(i) == list.get(t)) {
t++;
if (t == list.size()) {
int end = t;
System.out.println(list.get(i) + "-开始:" + i);
System.out.println(list.get(i) + "-结束:" + --end);
i = t;
break;
}
} else {
int end = t;
System.out.println(list.get(i) + "-开始:" + i);
System.out.println(list.get(i) + "-结束:" + --end);
i = t;
break;
}
}
}
}