如题。list里面也是list。怎么能够将有交集的list取出来。例如 [1,2,3,6],[2,3,4][3,6],[4,5]
这样子,最后取出来的是[1,2,3,6],[2,3,4],[4,5],而[3,6] 是被包含在[1,2,3,6]中的,就排出。
Java List<List<Integer>> 取有交集的元素的集合
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 阿进的写字台 2018-12-17 12:06关注
public void test(){ List<List<Integer>> result = new LinkedList<>(); result.add(new LinkedList<>(Arrays.asList(1, 2, 3, 6))); result.add(new LinkedList<>(Arrays.asList(2, 3, 4))); result.add(new LinkedList<>(Arrays.asList(3, 6))); result.add(new LinkedList<>(Arrays.asList(4, 5))); int sub[] = new int[result.size()]; for (int i = 0; i < result.size(); i++) { List<Integer> item = result.get(i); for (int k = i + 1; k < result.size(); k++) { List<Integer> OtherItem = result.get(k); if (sub[k]!=1 && item.containsAll(OtherItem)) { sub[k] = 1; } else if (sub[i]!=1 && OtherItem.containsAll(item)) { sub[i] = 1; } } } // 移除标记的元素 for (int i = sub.length - 1; i > 0; i--) { if (sub[i] == 1) { result.remove(i); } } for (int i = 0; i < result.size(); i++) { System.out.println(Arrays.toString(result.get(i).toArray())); } }
有更好的, 请告知。。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥40 复杂的限制性的商函数处理
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码