比較兩個數組中不同的值

 


import java.util.LinkedList;


public class MainC {
 public static void main(String[] args) {
  //  int[] a = { 1, 2, 3, 4 };
  //  int[] b = { 0, 1, 5, 3 };
  //  int[] c = new int[a.length+b.length];
  //  int[] d= new int[a.length+b.length];
  //  for(int n=0;n<a.length;n++)
  //  {
  //   c[n]=a[n];
  //  }
  //  for(int k=a.length;k<c.length;k++)
  //  {
  //   c[k]=b[k-a.length];
  //  }
  //  int z = 0;
  //  for (int x = 0; x < a.length; x++)
  //  {
  //   for (int y = 0; y < b.length; y++)
  //   {
  //    if (a[x] == b[y])
  //    {
  //     d[z]=a[x];
  //     z++;
  //    }
  //   }
  //  }
  //  for (int m = 0; m < c.length; m++)
  //  {
  //   for(int h=0;h<d.length;h++)
  //   {
  //    if(d[h]!=0)
  //    {
  //     if (c[m]== d[h])
  //     {
  //      c[m]=-1;
  //     }
  //    }
  //   }
  //  }
  //  for(int j=0;j<c.length;j++)
  //  {
  //   if(c[j]!=-1)
  //   {
  //    System.out.println(c[j]);
  //   }
  //  }

  String[] arr1 = new String[]{"1","2","3","7","9"};

  String[] arr2 = new String[]{"1","2","3","7"};

 

  String[] arro = arrContrast(arr1, arr2);//“arro”就是您想得到的數組

  System.out.println("您想要的結果爲:");

  for (String str : arro) {

   System.out.println(str);

  }

 


 }

 private static String[] arrContrast(String[] arr1, String[] arr2) {

  LinkedList<String> list = new LinkedList<String>();

  for (String str : arr1) {

   if (!list.contains(str)) {

    list.add(str);

   }

  }

  for (String str : arr2) {

   if (list.contains(str)) {

    list.remove(str);

   }

  }

  String[] result = {};

  return list.toArray(result);

 }
 

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章