Intersection and union of ArrayLists in Java

后端 未结 24 2175
离开以前
离开以前 2020-11-22 06:05

Are there any methods to do so? I was looking but couldn\'t find any.

Another question: I need these methods so I can filter files. Some are AND filter

24条回答
  •  伪装坚强ぢ
    2020-11-22 06:46

    If the number matches than I am checking it's occur first time or not with help of "indexOf()" if the number matches first time then print and save into in a string so, that when the next time same number matches then it's won't print because due to "indexOf()" condition will be false.

    class Intersection
    {
    public static void main(String[] args)
     {
      String s="";
        int[] array1 = {1, 2, 5, 5, 8, 9, 7,2,3512451,4,4,5 ,10};
        int[] array2 = {1, 0, 6, 15, 6, 5,4, 1,7, 0,5,4,5,2,3,8,5,3512451};
    
    
           for (int i = 0; i < array1.length; i++)
           {
               for (int j = 0; j < array2.length; j++)
               {
                   char c=(char)(array1[i]);
                   if(array1[i] == (array2[j])&&s.indexOf(c)==-1)
                   {    
                    System.out.println("Common element is : "+(array1[i]));
                    s+=c;
                    }
               }
           }    
    }
    

    }

提交回复
热议问题