Java查看一个子集合在集合中的位置
下文笔者讲述使用java代码检索集合的位置的方法分享,如下所示
子集合在集合中出现的位置的示例分享
实现思路: 使用Collections.indexOfSublist 或 使用Collections.lastIndexOfSubList 可查找子集合在集合中的第一次出现的位置和最后一次出现的位置例:
子集合在集合中出现的位置的示例分享
/** * java265.com 子集合在集合中第一次出现的位置的示例 * * @throws IOException */ public static void main(String[] args) throws IOException { List list = Arrays.asList("a b c d e f g".split(" ")); System.out.println("List :" + list); List sublist = Arrays.asList("d e".split(" ")); System.out.println("子列表 :" + sublist); System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist)); System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist)); } ------运行以上代码,将输出以下信息----- List :[a, b, c, d, e, f, g] 子列表 :[d, e] indexOfSubList: 3 lastIndexOfSubList: 3

版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。