亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

在Java中利用set特性刪除重復(fù)的數(shù)組元素

系統(tǒng) 2276 0

在Java中利用set特性刪除重復(fù)的數(shù)組元素

  Set(): 存入Set的每個(gè)元素必須是唯一的,因?yàn)镾et不保存重復(fù)元素。加入Set的Object必須定義equals()方法以確保對象的唯一性。Set與Collection有完

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

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

  TreeSet: 保持次序的Set,底層為樹結(jié)構(gòu)。使用它可以從Set中提取有序的序列。

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

  HashSet采用散列函數(shù)對元素進(jìn)行排序,這是專門為快速查詢而設(shè)計(jì)的;TreeSet采用紅黑樹的數(shù)據(jù)結(jié)構(gòu)進(jìn)行排序元素;LinkedHashSet內(nèi)部使用散列以加快查

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

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

?

Java代碼 復(fù)制代碼
  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. ?? //?將數(shù)組轉(zhuǎn)換成list ??
  13. ?? for ?( int ?i?=? 0 ;?i?<?a1.length;?i++)?{ ??
  14. ???list1.add(a1[i]); ??
  15. ??} ??
  16. ?? //?將數(shù)組轉(zhuǎn)換成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. ?? //?根據(jù)list中長度最長的設(shè)置list要循環(huán)的長度 ??
  25. ?? if ?(size_1?>=?size_2)?{ ??
  26. ??? //?逐個(gè)比較兩個(gè)list中的值是否相同 ??
  27. ??? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
  28. ????Integer?temp?=?list1.get(i); ??
  29. ???????????????? ??
  30. ???? //?如果兩個(gè)數(shù)組中有相同的值 ??
  31. ???? //?則將此值在兩個(gè)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. ??? //?逐個(gè)比較兩個(gè)list中的值是否相同 ??
  41. ??? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
  42. ????Integer?temp?=?list1.get(i); ??
  43. ??
  44. ???? //?如果兩個(gè)數(shù)組中有相同的值 ??
  45. ???? //?則將此值在兩個(gè)list中刪除 ??
  46. ???? //??注意此處不能使用remove方法 ??
  47. ???? if ?(list1.contains(temp))?{ ??
  48. ?????list1.remove(temp); ??
  49. ?????list2.remove(temp); ??
  50. ????} ??
  51. ???} ??
  52. ??} ??
  53. ??
  54. ??System.out.println( "???剩余的數(shù)組的信息?list1?:" ); ??
  55. ?? for ?( int ?i?=? 0 ;?i?<?list1.size();?i++)?{ ??
  56. ???System.out.println( "-----------------?:??" ?+?list1.get(i)); ??
  57. ??} ??
  58. ??
  59. ??System.out.println( "???剩余的數(shù)組的信息?list2?:" ); ??
  60. ?? for ?( int ?i?=? 0 ;?i?<?list2.size();?i++)?{ ??
  61. ???System.out.println( "-----------------?:??" ?+?list2.get(i)); ??
  62. ??} ??
  63. ??
  64. ?? //?將刪除掉重復(fù)元素的兩個(gè)list合并到第三個(gè)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>();

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

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

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

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

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

  // 將刪除掉重復(fù)元素的兩個(gè)list合并到第三個(gè)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代碼 復(fù)制代碼
  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. ??? //?因?yàn)槲覀円@取的是set集合,所以 ??
  17. ??? //?只在此處設(shè)置map的key ??
  18. ???map.put(a1[i],? null ); ??
  19. ??} ??
  20. ??
  21. ?? for ?( int ?i?=? 0 ;?i?<?a2.length;?i++)?{ ??
  22. ??? //?因?yàn)槲覀円@取的是set集合,所以 ??
  23. ??? //?只在此處設(shè)置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集合轉(zhuǎn)換成Integer數(shù)組 ??
  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特性刪除重復(fù)的數(shù)組元素


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色偷偷亚洲女人天堂观看欧 | 免费的性生活视频 | 视频在线一区二区 | 视频黄在线观看 | 久久综合偷偷噜噜噜色 | 动漫精品欧美一区二区三区 | 国内高清久久久久久久久 | 人人天天夜夜 | 欧美成人精品免费播放 | 亚洲综合国产精品 | 国产日日干 | 色噜噜狠狠色综合免费视频 | 精品久久久久久久中文字幕 | 伊人久久大香 | 国产精品66 | 色综合久久综合欧美综合网 | 99精品视频在线 | 一区二区在线精品免费视频 | 国产欧美在线播放 | 91成人午夜性a一级毛片 | 久爱www成人网免费视频 | 精产网红自拍在线 | 天天狠天天操 | 国产成人18黄网站免费 | 一区二区三区欧美日韩国产 | 日韩欧美亚洲综合久久影院d3 | 久久毛片网站 | 亚洲综合国产一区在线 | 国产xxxx做受性欧美88 | 精品久久久中文字幕一区 | 天天操综合视频 | 不卡一区二区在线观看 | 国产精品亚洲综合第一区 | 国产亚洲欧美久久久久 | 麻豆狠色伊人亚洲综合网站 | 免费一级毛片在播放视频 | 欧美视频在线观看一区二区 | 91精品国产免费久久久久久 | 亚洲欧美日韩高清一区二区一 | 九九九精品视频 | 免费一级片在线 |