Map案例
使用map接口,完成模仿斗地主的发牌和遍历牌
import java.util.*; public class ReadyPau { public static Map<Integer,String> ready(List<Integer> list){ Map<Integer,String> allpai = new HashMap<>(); String[] colar ={"♠","♥","♣","♦"}; String[] numbers={"3","4","5","6","7","8","9","10","J","Q", "K","A","2",}; Integer count = 0; for(int i= 0;i<colar.length;i++){ for(int j=0;j<numbers.length;j++){ list.add(count); String str = colar[i]+" "+numbers[j]; allpai.put(count,str); count++; } } allpai.put(count++,"大王"); list.add(count); allpai.put(count++,"小王"); list.add(count); return allpai; } public static void fapai(List<Integer> list,List<Integer> list1,List<Integer> list2,List<Integer> list3, List<Integer> list4){ for(int i=0;i<list.size();i++){ if (i>=51){ list4.add(i); } if(i%3==0){ list1.add(i); } if(i%3==1){ list2.add(i); } if(i%3==2){ list3.add(i); } } } public static void kanpai(String name,List<Integer> lidt,Map<Integer,String> map){ System.out.println(name+":"); for (Integer key : lidt) { String s = map.get(key); System.out.print(s+" "); } System.out.println(); } }
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Apply { public static void main(String[] args) { Map<Integer,String> allpai = new HashMap<>(); ArrayList<Integer> play = new ArrayList<>(); ArrayList<Integer> player01 = new ArrayList<>(); ArrayList<Integer> player02 = new ArrayList<>(); ArrayList<Integer> player03 = new ArrayList<>(); ArrayList<Integer> dipai = new ArrayList<>(); allpai=ReadyPau.ready(play); Collections.shuffle(play); ReadyPau.fapai(play,player01,player02,player03,dipai); Collections.sort(player01); Collections.sort(player02); Collections.sort(player03); ReadyPau.kanpai("01",player01,allpai); ReadyPau.kanpai("02",player02,allpai); ReadyPau.kanpai("03",player03,allpai); ReadyPau.kanpai("dipai",dipai,allpai); } }