欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

在Java中利用set特性刪除重復的數組元素

系統 2365 0

在Java中利用set特性刪除重復的數組元素

  Set(): 存入Set的每個元素必須是唯一的,因為Set不保存重復元素。加入Set的Object必須定義equals()方法以確保對象的唯一性。Set與Collection有完

全一樣的接口。Set接口不保證維護元素的次序。

  HashSet: 為快速查找而設計的Set。存入HashSet的對象必須定義hashCode()。

  TreeSet: 保持次序的Set,底層為樹結構。使用它可以從Set中提取有序的序列。

  LinkedHashSet: 具有HashSet的查詢速度,且內部使用鏈表維護元素的順序(插入的次序)。于是在使用迭代器遍歷Set時,結果會按元素插入的次序顯示。

  HashSet采用散列函數對元素進行排序,這是專門為快速查詢而設計的;TreeSet采用紅黑樹的數據結構進行排序元素;LinkedHashSet內部使用散列以加快查

詢速度,同時使用鏈表維護元素的次序,使得看起來元素是以插入的順序保存的。需要注意的是,生成自己的類時,Set需要維護元素的存儲順序,因此要實現

Comparable接口并定義compareTo()方法。

?

Java代碼 復制代碼
  1. import ?java.util.LinkedList; ??
  2. ??
  3. public ? class ?TestArray01?{ ??
  4. ? ??
  5. ? public ? static ? void ?main(String?args[])?{ ??
  6. ??
  7. ??Integer[]?a1?=?{? 1 ,? 2 ,? 4 ,? 3 ,? 5 ,? 7 ,? 8 ?}; ??
  8. ??Integer[]?a2?=?{? 8 ,? 2 ,? 4 ,? 7 ,? 9 ?}; ??
  9. ??LinkedList<Integer>?list1?=? new ?LinkedList<Integer>(); ??
  10. ??LinkedList<Integer>?list2?=? new ?LinkedList<Integer>(); ??
  11. ??
  12. ?? //?將數組轉換成list ??
  13. ?? for ?( int ?i?=? 0 ;?i?<?a1.length;?i++)?{ ??
  14. ???list1.add(a1[i]); ??
  15. ??} ??
  16. ?? //?將數組轉換成list ??
  17. ?? for ?( int ?i?=? 0 ;?i?<?a2.length;?i++)?{ ??
  18. ???list2.add(a2[i]); ??
  19. ??} ??
  20. ??
  21. ?? int ?size_1?=?list1.size(); ??
  22. ?? int ?size_2?=?list2.size(); ??
  23. ?? ??
  24. ?? //?根據list中長度最長的設置list要循環的長度 ??
  25. ?? if ?(size_1?>=?size_2)?{ ??
  26. ??? //?逐個比較兩個list中的值是否相同 ??
  27. ??? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
  28. ????Integer?temp?=?list1.get(i); ??
  29. ???????????????? ??
  30. ???? //?如果兩個數組中有相同的值 ??
  31. ???? //?則將此值在兩個list中刪除 ??
  32. ???? //??注意此處不能使用remove方法 ??
  33. ???? if ?(list2.contains(temp))?{ ??
  34. ?????list1.set(i,? null ); ??
  35. ????? int ?pos?=?list2.indexOf(temp); ??
  36. ?????list2.set(pos,? null ); ??
  37. ????} ??
  38. ???} ??
  39. ??}? else ?{ ??
  40. ??? //?逐個比較兩個list中的值是否相同 ??
  41. ??? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
  42. ????Integer?temp?=?list1.get(i); ??
  43. ??
  44. ???? //?如果兩個數組中有相同的值 ??
  45. ???? //?則將此值在兩個list中刪除 ??
  46. ???? //??注意此處不能使用remove方法 ??
  47. ???? if ?(list1.contains(temp))?{ ??
  48. ?????list1.remove(temp); ??
  49. ?????list2.remove(temp); ??
  50. ????} ??
  51. ???} ??
  52. ??} ??
  53. ??
  54. ??System.out.println( "???剩余的數組的信息?list1?:" ); ??
  55. ?? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
  56. ???System.out.println( "-----------------?:??" ?+?list1.get(i)); ??
  57. ??} ??
  58. ??
  59. ??System.out.println( "???剩余的數組的信息?list2?:" ); ??
  60. ?? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
  61. ???System.out.println( "-----------------?:??" ?+?list2.get(i)); ??
  62. ??} ??
  63. ??
  64. ?? //?將刪除掉重復元素的兩個list合并到第三個list中 ??
  65. ??LinkedList<Integer>?list3?=? new ?LinkedList<Integer>(); ??
  66. ?? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
  67. ??
  68. ??? if ?(list1.get(i)?!=? null )?{ ??
  69. ????list3.addLast(list1.get(i)); ??
  70. ???} ??
  71. ??} ??
  72. ?? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
  73. ??? if ?(list2.get(i)?!=? null )?{ ??
  74. ????list3.addLast(list2.get(i)); ??
  75. ???} ??
  76. ??} ??
  77. ??
  78. ?? for ?( int ?i?=? 0 ;?i?<?list3.size();?i++)?{ ??
  79. ???System.out.println( "-------list3----------?:??" ?+?list3.get(i)); ??
  80. ??} ??
  81. ?} ??
  82. }??
      import java.util.LinkedList;

public class TestArray01 {
 
 public static void main(String args[]) {

  Integer[] a1 = { 1, 2, 4, 3, 5, 7, 8 };
  Integer[] a2 = { 8, 2, 4, 7, 9 };
  LinkedList<Integer> list1 = new LinkedList<Integer>();
  LinkedList<Integer> list2 = new LinkedList<Integer>();

  // 將數組轉換成list
  for (int i = 0; i < a1.length; i++) {
   list1.add(a1[i]);
  }
  // 將數組轉換成list
  for (int i = 0; i < a2.length; i++) {
   list2.add(a2[i]);
  }

  int size_1 = list1.size();
  int size_2 = list2.size();
  
  // 根據list中長度最長的設置list要循環的長度
  if (size_1 >= size_2) {
   // 逐個比較兩個list中的值是否相同
   for (int i = 0; i < list1.size(); i++) {
    Integer temp = list1.get(i);
                
    // 如果兩個數組中有相同的值
    // 則將此值在兩個list中刪除
    //  注意此處不能使用remove方法
    if (list2.contains(temp)) {
     list1.set(i, null);
     int pos = list2.indexOf(temp);
     list2.set(pos, null);
    }
   }
  } else {
   // 逐個比較兩個list中的值是否相同
   for (int i = 0; i < list2.size(); i++) {
    Integer temp = list1.get(i);

    // 如果兩個數組中有相同的值
    // 則將此值在兩個list中刪除
    //  注意此處不能使用remove方法
    if (list1.contains(temp)) {
     list1.remove(temp);
     list2.remove(temp);
    }
   }
  }

  System.out.println("   剩余的數組的信息 list1 :");
  for (int i = 0; i < list1.size(); i++) {
   System.out.println("----------------- :  " + list1.get(i));
  }

  System.out.println("   剩余的數組的信息 list2 :");
  for (int i = 0; i < list2.size(); i++) {
   System.out.println("----------------- :  " + list2.get(i));
  }

  // 將刪除掉重復元素的兩個list合并到第三個list中
  LinkedList<Integer> list3 = new LinkedList<Integer>();
  for (int i = 0; i < list1.size(); i++) {

   if (list1.get(i) != null) {
    list3.addLast(list1.get(i));
   }
  }
  for (int i = 0; i < list2.size(); i++) {
   if (list2.get(i) != null) {
    list3.addLast(list2.get(i));
   }
  }

  for (int i = 0; i < list3.size(); i++) {
   System.out.println("-------list3---------- :  " + list3.get(i));
  }
 }
}

    

?

?

Java代碼 復制代碼
  1. import ?java.util.ArrayList; ??
  2. import ?java.util.HashMap; ??
  3. import ?java.util.Iterator; ??
  4. import ?java.util.List; ??
  5. import ?java.util.Map; ??
  6. ??
  7. public ? class ?TestArray02?{ ??
  8. ??
  9. ? public ? static ? void ?main(String?args[])?{ ??
  10. ??Integer[]?a1?=?{? 1 ,? 2 ,? 3 ?}; ??
  11. ??Integer[]?a2?=?{? 4 ,? 2 ,? 3 ?}; ??
  12. ??
  13. ??Map<Integer,?Object>?map?=? new ?HashMap<Integer,?Object>(); ??
  14. ??
  15. ?? for ?( int ?i?=? 0 ;?i?<?a1.length;?i++)?{ ??
  16. ??? //?因為我們要獲取的是set集合,所以 ??
  17. ??? //?只在此處設置map的key ??
  18. ???map.put(a1[i],? null ); ??
  19. ??} ??
  20. ??
  21. ?? for ?( int ?i?=? 0 ;?i?<?a2.length;?i++)?{ ??
  22. ??? //?因為我們要獲取的是set集合,所以 ??
  23. ??? //?只在此處設置map的key ??
  24. ???map.put(a2[i],? null ); ??
  25. ??} ??
  26. ??
  27. ??List<Integer>?list?=? new ?ArrayList<Integer>(); ??
  28. ??Iterator<Integer>?it?=?map.keySet().iterator(); //?在此處獲取set的集合 ??
  29. ?? while ?(it.hasNext())?{ ??
  30. ???Integer?ob?=?(Integer)?it.next(); ??
  31. ???list.add(ob); ??
  32. ??} ??
  33. ??
  34. ?? //?將list集合轉換成Integer數組 ??
  35. ??Integer[]?a3?=?(Integer[])?list.toArray( new ?Integer[list.size()]); ??
  36. ??
  37. ?? for ?( int ?i?=? 0 ;?i?<?a3.length;?i++)?{ ??
  38. ???System.out.println(a3[i]); ??
  39. ??} ??
  40. ?} ??
  41. }?

在Java中利用set特性刪除重復的數組元素


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: www操com| jzz 护士| 天堂在线网 | 一级毛片在线完整免费观看 | 琪琪五月天综合婷婷 | 一区二区三区四区国产 | 久久99热这里只频精品6中文字幕 | 国产精品久久久久久久一区探花 | 久色乳综合思思在线视频 | 久草免费在线观看 | 久久久久久国产精品mv | 欧美午夜一艳片欧美精品 | 亚洲综合色视频在线观看 | 亚洲综人网 | 人妻体内射精一区二区三四 | 国产美女精品 | 天天操欧美 | 欧美第一页草草影院浮力 | 欧美国产激情二区三区 | 日韩极品视频 | 国产毛片av | 欧美一级二级在线观看 | 久久久999| 国产成人黄网址在线视频 | 国产一级毛片高清 | 人人艹逼 | 精品久久久久久久 | 男人j进女人j啪啪无遮挡动态 | 久久精品国产一区二区三区不卡 | 激情成人综合网 | 五月婷婷色综合 | 亚洲天堂av在线 | 日韩www| 请吃饭的姐姐 | 欧美日韩国产综合一区二区三区 | 二区视频 | 免费观看一级特黄欧美大片 | 伦一区二区三区中文字幕v亚洲 | 92精品国产自产在线 | 久久亚洲最大成人网4438 | 一级片在线观看 |