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);
}
}