猿问

如何比较两个不同大小的数组列表并检查元素的顺序

如何比较两个不同大小的数组列表并检查元素的顺序?


1.首先检查 ArrayList1 是 list2 的子集


2.第二次检查ArrayList2与List1的顺序相同,忽略随机元素


Arraylist1{"this", "is", "a", "coding", "test"};

Arraylist2{"this", "is", "random1", "a", "random2", "coding", "random3", "test"};

如果 List2 按以下顺序测试将通过:


"this" "is" "a" "coding" "test" 

如果列表 2 有任何其他顺序,则测试将失败,例如:


"a", "is", "coding", "test", "this",或者如果缺少这 5 个单词中的任何一个。


程序应忽略列表 2 中任意数量的随机值(如 random1、random2 和 random3)。


我怎样才能实现这种情况?


我尝试for了循环和迭代器。它没有用,他们给了我两个 ArrayList 的共同元素,但没有给我“订单”。我还可以做些什么?


for循环使用:


list1.contains(list2.get(i)))

但这只是比较值,不检查顺序。


while带循环的迭代器:


Iterator<String> List1_Iterator = List1.iterator();



while (List1_Iterator.hasNext()) {

}

这也不检查元素的顺序。


白板的微信
浏览 312回答 3
3回答

MYYA

您也可以按照您的步骤执行这个简单的过程。1)比较元素。2)比较顺序。import java.util.*;class Stack1{&nbsp; &nbsp; public static void main(String args[])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; list.add("First");&nbsp; &nbsp; &nbsp; &nbsp; list.add("name");&nbsp; &nbsp; &nbsp; &nbsp; list.add("is");&nbsp; &nbsp; &nbsp; &nbsp; list.add("Jay");&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list2=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; list2.add("First");&nbsp; &nbsp; &nbsp; &nbsp; list2.add("name");&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; list2.add("is");&nbsp; &nbsp; &nbsp; &nbsp; list2.add("Sudeep");&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> list3=new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<list2.size();i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=0;j<list.size();j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(list2.contains(list.get(j))==true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(list2.get(i)==list.get(j))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list3.add(list2.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{ break; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(list.equals(list3))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("true");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{System.out.println("false");}&nbsp; &nbsp; }}

翻过高山走不出你

一种方法是首先创建一个名为 2 的列表的副本list2Copy,然后删除list2Copy中不存在的所有元素list1。现在你只需要比较它们是否完全相等。List<Integer> list1 = Arrays.asList(1,3,5);List<Integer> list2 = Arrays.asList(1,2,3,4,5);ArrayList<Integer> list2Copy = new ArrayList<>(list2);list2Copy.removeIf(x -> !list1.contains(x));return list1.equals(list2Copy);这是另一种时间复杂度较小的方法:if (list1.size() > list2.size()) {&nbsp; &nbsp; // definitely not same order&nbsp; &nbsp; return false;}int list1Index = 0;for (int i = 0 ; i < list2.size() ; i++) {&nbsp; &nbsp; if (Objects.equals(list2.get(i), list1.get(list1Index))) {&nbsp; &nbsp; &nbsp; &nbsp; list1Index++;&nbsp; &nbsp; &nbsp; &nbsp; if (list1Index == list1.size())&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// at the end, list1Index should be the same as list1.size() if list2 is in the same order.return false;

江户川乱折腾

Arraylist 是索引的,因此您可以遍历最小的列表,然后通过比较索引处的值来检查不匹配。只有当两个列表中的元素顺序正确(一个列表是另一个列表的子集,并且元素以相同的顺序相同)时,才能以有效的方式执行相同操作的方法才会返回 true。private boolean checkForEqualityInorder(List<String> list1, List<String> list2) {&nbsp; &nbsp; &nbsp; &nbsp; if (list1.size() < list2.size()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i <list1.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( !list1.get(i).equals(list2.get(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i <list2.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( !list2.get(i).equals(list1.get(i))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }上面的方法接受两个列表,并且只有当一个是另一个的子集时才会返回 true(按顺序检查)。为您的问题增强相同的方法:private boolean checkForEqualityInorder(List<String> list1, List<String> list2) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0, k=0; i <list1.size(); i++, k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (list2.get(k).startsWith("random")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!list1.get(i).equals(list2.get(k))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答