简体   繁体   English

通过arraylist打印列表2中的值

[英]Print values in list 2 through arraylist

i have 2 lists list 1 and list2 now i need to do a search in list 2 compared to list 1 if that element exisists i have written down the code but it is not printing? 我有两个列表1和列表2,现在我需要在列表2中进行搜索,如果列表中存在该元素,那么我已经写下了代码,但是没有打印出来,所以与列表1相比?

Iterator<String> it=list1.iterator();
      while(it.hasNext())
      {
          if(list2.contains(it.next()))
          {
            System.out.println(list2);

          }


      }

Some options: 一些选项:

  • At least one of your lists is empty. 您的列表中至少有一个是空的。
  • They have no objects in common. 它们没有共同的对象。
  • The class which you use does not implement equals correctly. 您使用的类未正确实现equals

try 尝试

for(String s : list1){
   if(list2.contains(s)){
          System.out.println(s);
    }
}

based on that both lists contain String objects 基于这两个列表都包含String对象

Make sure neither list is empty and the definitely have something in common, then try: 确保两个列表都不为空并且肯定有相同之处,然后尝试:

for(String str in list1)
    if(list2.contains(str))
        System.out.println(str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM