set中treeset()方法的用法+java中 try和catch的用法

set中treeset()方法的用法+java中 try和catch的用法

1、set中treeset()方法的用法

代码区如果有错误,就会返回所写异常的处理。

首先要清楚,如果没有try的话,出现异常会导致程序崩溃。而try则可以保证程序的正常运行下去,不管try和catch里面执行哪一个,都要在最后执行final,比如说:

try
{
    int i = 1/0;
}
catch(Exception e)
{
    ........
}final{

}
2、try和catch的用法

TreeSet的性能比 HashSet差但是我们 在需要排序的时候可以用TreeSet 因为他是
自然排序也就是 升序
下面是TreeSet实现代码
这个类也似只能通过迭代器迭代元素

package com.zhongruan;

import java.util.*;

public class DDZ2 {
    public static void main(String[] args){
        Map<Integer,String> poker=new HashMap<>();
        String[] nums={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors={"♠","♥","♣","♦"};
        int index=0;
        for (String num:nums){
            for (String color:colors){
                String pai=color+num;
                index++;
                poker.put(index,pai);
            }
        }
        index++;
        poker.put(index,"大王");
        index++;
        poker.put(index,"小王");
        System.out.println(poker);
        List<Integer> pokerIndexs=new ArrayList<>();
        Set<Integer> integers=poker.keySet();
        for(Integer i:poker.keySet()){
            pokerIndexs.add(i);
        }
        Collections.shuffle(pokerIndexs);
        System.out.println(pokerIndexs);

        Set<Integer> dipaiIns=new TreeSet<>();
        dipaiIns.add(pokerIndexs.remove(0));
        dipaiIns.add(pokerIndexs.remove(0));
        dipaiIns.add(pokerIndexs.remove(0));
        System.out.println(dipaiIns);
        System.out.println(pokerIndexs);
        Set<Integer> ct=new TreeSet<>();
        Set<Integer> wwc=new TreeSet<>();
        Set<Integer> sj=new TreeSet<>();
        for (int i=0;i<pokerIndexs.size();i++){
            int pi=pokerIndexs.get(i);
            int mod=i%3;
            if(mod==0){
                ct.add(pi);
            }else if (mod==1){
                wwc.add(pi);
            }
            else {
                sj.add(pi);
            }
        }
        System.out.println(ct);
        System.out.println(wwc);
        System.out.println(sj);
         look(poker,ct);
        look(poker,wwc);
        look(poker,sj);



    }
    public static void look(Map<Integer,String> poker,Set<Integer> indexs){
        List<String> p=new ArrayList<>();
        for (Integer i:indexs){
            String pai=poker.get(i);
            p.add(pai);

        }
        System.out.println(p);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值