加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java_Collections工具类

发布时间:2020-12-14 06:26:23 所属栏目:Java 来源:网络整理
导读:Collections 工具类 * Collection与Collections区别 Collection 接口,(大部分集合类的实现接口) Collections 工具类(针对列表) ? ? ? ? * Collections工具类常用方法 1 2span style="color: #000000".sort(List list)排序 3span style="color: #000000".sort

Collections 工具类

  * Collection与Collections区别

    Collection 接口,(大部分集合类的实现接口)

    Collections 工具类(针对列表)

? ? ? ? * Collections工具类常用方法  

   1   2<span style="color: #000000">.sort(List list)排序

   3<span style="color: #000000">.sort(List list,Comparator comparator);排序

   4<span style="color: #000000">.binarySearch,搜索指定元素索引,前提列表必须有序

   5<span style="color: #000000">.reverse反转

   6<span style="color: #000000">.重新洗牌shuffle方法

   7.swap交换,交换两个索引位置元素,Collections.swap(list,1,2);

  * 测试Collections工具类方法代码:  

<span style="color: #0000ff">import<span style="color: #000000"> java.util.ArrayList;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Arrays;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Collections;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Comparator;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.List;
<span style="color: #008000">/*<span style="color: #008000">

  • 1.父类的引用指向子类的对象

  • 2.sort(List list)排序

  • 3.sort(List list,Comparator comparator);排序

  • 4.binarySearch,前提列表必须有序

  • 5.reverse反转

  • 6.重新洗牌shuffle

  • 7.swap交换,2);

  • <span style="color: #808080">@author<span style="color: #008000"> Administrator

  • <span style="color: #008000">*/
    <span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> CollectionsDemo {
    <span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
    List list=<span style="color: #0000ff">new<span style="color: #000000"> ArrayList();

     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;添加测试数据        </span>
     list.add("a"<span style="color: #000000"&gt;);
     list.add(</span>"b"<span style="color: #000000"&gt;);
     list.add(</span>"d"<span style="color: #000000"&gt;);
     list.add(</span>"c"<span style="color: #000000"&gt;);
     list.add(</span>"e"<span style="color: #000000"&gt;);
    
     System.out.println(Arrays.toString(list.toArray()));
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;sort排序,升序</span>

    <span style="color: #000000"> Collections.sort(list);

     System.out.println(Arrays.toString(list.toArray()));
    
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;使用sort降序排序</span>
     Collections.sort(list,<span style="color: #0000ff"&gt;new</span> Comparator<String><span style="color: #000000"&gt;(){
    
         @Override
         </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;int</span><span style="color: #000000"&gt; compare(String o1,String o2) {
             </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub</span>
             <span style="color: #0000ff"&gt;int</span> result=o2.length()-<span style="color: #000000"&gt;o1.length();
             </span><span style="color: #0000ff"&gt;int</span> length=result>0?<span style="color: #000000"&gt;o2.length():o1.length();
    
             </span><span style="color: #0000ff"&gt;char</span>[]a1=<span style="color: #000000"&gt;o1.toCharArray();
             </span><span style="color: #0000ff"&gt;char</span>[]a2=<span style="color: #000000"&gt;o2.toCharArray();
             </span><span style="color: #0000ff"&gt;for</span>(<span style="color: #0000ff"&gt;int</span> i=0;i<length;i++<span style="color: #000000"&gt;){
                 </span><span style="color: #0000ff"&gt;if</span>(a1[i]><span style="color: #000000"&gt;a2[i]){
                     </span><span style="color: #0000ff"&gt;return</span> -1<span style="color: #000000"&gt;;
                 }</span><span style="color: #0000ff"&gt;else</span> <span style="color: #0000ff"&gt;if</span>(a1[i]==<span style="color: #000000"&gt;a2[i]){
                     </span><span style="color: #0000ff"&gt;return</span> 0<span style="color: #000000"&gt;;
                 }</span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt;{
                     </span><span style="color: #0000ff"&gt;return</span> 1<span style="color: #000000"&gt;;
                 }
             }
             </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; result;
         }
     });
    
     System.out.println(Arrays.toString(list.toArray()));
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;二分法检测元素索引</span>
     <span style="color: #0000ff"&gt;int</span> indexResult = Collections.binarySearch(list,"c"<span style="color: #000000"&gt;);
    
     System.out.println(indexResult);
    
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;reverse反转</span>

    <span style="color: #000000"> Collections.reverse(list);
    System.out.println(Arrays.toString(list.toArray()));

     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;重新洗牌shuffle</span>

    <span style="color: #000000"> Collections.shuffle(list);
    System.out.println(Arrays.toString(list.toArray()));

     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;swap交换,交换两个索引位置元素</span>
     Collections.swap(list,2<span style="color: #000000"&gt;);
     System.out.println(Arrays.toString(list.toArray()));

    }
    }

  * 模拟斗地主,发牌代码

<span style="color: #0000ff">import<span style="color: #000000"> java.util.ArrayList;
<span style="color: #0000ff">import<span style="color: #000000"> java.util.Collections;

<span style="color: #008000">/*<span style="color: #008000">

  • 模拟斗地主,洗牌

  • <span style="color: #808080">@author<span style="color: #008000"> Administrator

  • <span style="color: #008000">*/
    <span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> Collections_card {

    <span style="color: #0000ff">public <span style="color: #0000ff">static <span style="color: #0000ff">void<span style="color: #000000"> main(String[] args) {
    ArrayList cards = <span style="color: #0000ff">new<span style="color: #000000"> ArrayList();

     ArrayList play1 </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ArrayList();
     ArrayList play2 </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ArrayList();
     ArrayList play3 </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ArrayList();
    
     ArrayList dipai </span>= <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ArrayList();
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;54张牌</span>
     <span style="color: #0000ff"&gt;for</span>(<span style="color: #0000ff"&gt;int</span> i=0;i<54;i++<span style="color: #000000"&gt;){
         cards.add(i);
     }
     Collections.shuffle(cards);
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;三个玩家分牌</span>
     <span style="color: #0000ff"&gt;for</span>(<span style="color: #0000ff"&gt;int</span> i=0;i<51;i+=3<span style="color: #000000"&gt;){
         play1.add(cards.get(i));
         play2.add(cards.get(i</span>+1<span style="color: #000000"&gt;));
         play3.add(cards.get(i</span>+2<span style="color: #000000"&gt;));
     }
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;底牌</span>
     <span style="color: #0000ff"&gt;for</span>(<span style="color: #0000ff"&gt;int</span> i=51;i<54;i++<span style="color: #000000"&gt;){
         dipai.add(cards.get(i));
     }
    
     </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;输出检查效果</span>
     System.out.println("玩家一:"+<span style="color: #000000"&gt;play1);
     System.out.println(</span>"玩家二:"+<span style="color: #000000"&gt;play2);
     System.out.println(</span>"玩家三:"+<span style="color: #000000"&gt;play3);
     System.out.println(</span>"底牌:"+<span style="color: #000000"&gt;dipai);

    }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读