zoukankan      html  css  js  c++  java
  • Judge

    1. 循环list中的所有元素然后删除重复 

    [java] view plaincopy
     
    1. public   static   List  removeDuplicate(List list)  {     
    2.   for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {     
    3.       for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {     
    4.            if  (list.get(j).equals(list.get(i)))  {     
    5.               list.remove(j);     
    6.             }      
    7.         }      
    8.       }      
    9.     return list;     
    10. }    

    2. 通过HashSet踢除重复元素

    [c-sharp] view plaincopy
     
    1. public   static   List  removeDuplicate(List list)  {     
    2.     HashSet h  =   new  HashSet(list);     
    3.     list.clear();     
    4.     list.addAll(h);     
    5.     return list;     
    6. }     

    在groovy中当然也可以使用上面的两种方法, 但groovy自己提供了unique方法来去除重复数据 

    [c-sharp] view plaincopy
     
    1. def list = [1, 2, 3, 2, 4, 1, 5]     
    2. list.unique()  // [1, 2, 3, 4, 5]  
  • 相关阅读:
    Java使用默认浏览器打开指定URL
    eclipse.ini内存设置
    Eclipse关联Java源代码
    C#调用Java代码
    UVA12161 Ironman Race in Treeland
    [JSOI2012]玄武密码
    着色问题
    luogu P2680 运输计划
    [BJWC2010]严格次小生成树
    [SDOI2015]异象石
  • 原文地址:https://www.cnblogs.com/a757956132/p/4287812.html
Copyright © 2011-2022 走看看