java集合练习题
1.使用List集合存儲10個學生信息。
學生信息:姓名,年齡,成績。
統計所有姓“張”的同學的平均成績。
package week2.day5;public class Student {private String name;private int age;private int score;public Student() {}public Student(String name, int age, int score) {this.name = name;this.age = age;this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", score=" + score +'}';} } package week2.day5;import java.util.ArrayList; import java.util.List;public class KaoShi {public static void main(String[] args) {List <Student>l=new ArrayList<>();double sum=0;int b=0;l.add(new Student("張三",18,80));l.add(new Student("李三",18,80));l.add(new Student("是三",16,80));l.add(new Student("張四",18,89));l.add(new Student("的三",18,80));l.add(new Student("往三",18,87));l.add(new Student("來三",18,85));l.add(new Student("兒三",18,80));l.add(new Student("怕三",18,80));l.add(new Student("去三",18,80));for (int i = 0; i < l.size(); i++) {if (l.get(i).getName().startsWith("張")){b++;sum+=l.get(i).getScore();}}System.out.println("姓張的平均成績為:"+sum/b);} }?2.產生10個1-100的隨機數,并放到一個數組中,把數組中大于等于10的數字放到一個list集合中,并打印到控制臺。
public class Test1 {public static void main(String[] args) {int[] a=new int[10];Random r=new Random();for (int i = 0; i < a.length; i++) {int i1 = r.nextInt(100)+1;a[i]=i1;}List<Integer> list=new ArrayList<>();for (int i = 0; i < a.length; i++) {if(a[i]>10){list.add(a[i]);}}System.out.println(list);} }3.有2個數組,第一個數組內容為:[黑龍江省,浙江省,江西省,廣東省,福建省],第二個數組為:[哈爾濱,杭州,南昌,廣州,福州],將第一個數組元素作為key,第二個數組元素作為value存儲到Map集合中。如{黑龍江省=哈爾濱, 浙江省=杭州, …}。
public class Test2 {public static void main(String[] args) {String[] shengs={"黑龍江省","浙江省","江西省","廣東省","福建省"};String[] shis={"哈爾濱","杭州","南昌","廣州","福州"};Map<String,String> map=new HashMap<>();for (int i = 0; i < shengs.length; i++) {map.put(shengs[i],shis[i]);}Set<String> keys = map.keySet();for(String k:keys){String v = map.get(k);System.out.println(k+"="+v);}} }4.把如下元素存入List集合 “aaa” “bbb” “aaa” “abc”“xyz” “123” “xyz” 去掉重復元素
public class Test3 {public static void main(String[] args) {List<String> list=new ArrayList<>();list.add("aa");list.add("aa");list.add("bb");list.add("cc");list.add("bb");// Set<String> set=new HashSet<>(); // for (String s:list){ // set.add(s); // }Set<String> set=new HashSet<>(list);System.out.println(set);} }?5.定義一個泛型為String類型的List集合,統計該集合中每個字符(注意,不是字符串)出現的次數。例如:集合中有”abc”、”bcd”兩個元素,程序最終輸出結果為:“a = 1,b = 2,c = 2,d = 1”。
public class Test4 {public static void main(String[] args) {List<String> list=new ArrayList<>();list.add("ahkjfha");list.add("afdadfg");Map<Character,Integer> map=new HashMap<>();for (String s:list){char[] chars = s.toCharArray();for(char c:chars){Integer count = map.get(c);if(count==null){map.put(c,1);}else{count++;map.put(c,count);}}}Set<Character> keys = map.keySet();for(Character k:keys){Integer v = map.get(k);System.out.println(k+"="+v);}} }6.利用Map,完成下面的功能:
(1)從命令行讀入一個字符串,表示一個年份,輸出該年的世界杯冠軍是哪支球隊。如果該 年沒有舉辦世界杯,則輸出:沒有舉辦世界杯。
(2)在原有世界杯Map 的基礎上,增加如下功能: 讀入一支球隊的名字,輸出該球隊奪冠的年份列表。 例如,讀入“巴西”,應當輸出 1958 1962 1970 1994 2002 讀入“荷蘭”,應當輸出 沒有獲得過世界杯
package week2.day5.lx2;public class MapDemo {private int id;private int year;private String courty;private String team;public MapDemo(){}public MapDemo(int id, int year, String courty, String team) {this.id = id;this.year = year;this.courty = courty;this.team = team;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getYear() {return year;}public void setYear(int year) {this.year = year;}public String getCourty() {return courty;}public void setCourty(String courty) {this.courty = courty;}public String getTeam() {return team;}public void setTeam(String team) {this.team = team;}} public class Test5 {public static void main(String[] args) {MapDemo mapDemo1=new MapDemo(1,1930,"烏拉圭","烏拉圭");MapDemo mapDemo2=new MapDemo(2,1934,"意大利","意大利");MapDemo mapDemo3=new MapDemo(3,1938,"法國","意大利");MapDemo mapDemo4=new MapDemo(4,1950,"巴西","烏拉圭");MapDemo mapDemo5=new MapDemo(5,1954,"瑞士","西德");MapDemo mapDemo6=new MapDemo(6,1958,"瑞典","巴西");MapDemo mapDemo7=new MapDemo(7,1962,"智利","巴西");MapDemo mapDemo8=new MapDemo(8,1966,"英格蘭","英格蘭");MapDemo mapDemo9=new MapDemo(9,1970,"墨西哥","巴西");MapDemo mapDemo10=new MapDemo(10,1974,"前西德","西德");MapDemo mapDemo11=new MapDemo(11,1978,"阿根廷","阿根廷");MapDemo mapDemo12=new MapDemo(12,1982,"西班牙","意大利");MapDemo mapDemo13=new MapDemo(13,1986,"墨西哥","阿根廷");MapDemo mapDemo14=new MapDemo(14,1990,"意大利","西德");MapDemo mapDemo15=new MapDemo(15,1994,"美國","巴西");MapDemo mapDemo16=new MapDemo(16,1998,"法國","法國 ");MapDemo mapDemo17=new MapDemo(17,2002,"韓日","巴西");MapDemo mapDemo18=new MapDemo(18,2006,"德國","意大利");MapDemo mapDemo19=new MapDemo(19,2010,"南非","西班牙");MapDemo mapDemo20=new MapDemo(20,2014,"巴西","德國");Map<Integer,MapDemo> map=new HashMap<>();map.put(mapDemo1.getYear(),mapDemo1);map.put(mapDemo2.getYear(),mapDemo2);map.put(mapDemo3.getYear(),mapDemo3);map.put(mapDemo4.getYear(),mapDemo4);map.put(mapDemo5.getYear(),mapDemo5);map.put(mapDemo6.getYear(),mapDemo6);map.put(mapDemo7.getYear(),mapDemo7);map.put(mapDemo8.getYear(),mapDemo8);map.put(mapDemo9.getYear(),mapDemo9);map.put(mapDemo10.getYear(),mapDemo10);map.put(mapDemo11.getYear(),mapDemo11);map.put(mapDemo12.getYear(),mapDemo12);map.put(mapDemo13.getYear(),mapDemo13);map.put(mapDemo14.getYear(),mapDemo14);map.put(mapDemo15.getYear(),mapDemo15);map.put(mapDemo16.getYear(),mapDemo16);map.put(mapDemo17.getYear(),mapDemo17);map.put(mapDemo18.getYear(),mapDemo18);map.put(mapDemo19.getYear(),mapDemo19);map.put(mapDemo20.getYear(),mapDemo20);// 1、根據年份找冠軍Scanner scanner=new Scanner(System.in);int y = scanner.nextInt();MapDemo mapDemo = map.get(y);if(mapDemo==null){System.out.println("該年沒有舉辦世界杯");}else{System.out.println("冠軍是:"+mapDemo.getTeam());}// 2\根據冠軍找年份Scanner scanner2=new Scanner(System.in);String team = scanner2.nextLine();Set<Integer> keys = map.keySet();boolean flag=false;for(Integer k:keys){MapDemo v=map.get(k);if(v.getTeam().equals(team)){flag=true;System.out.println(k);}}if(flag==false){System.out.println("無冕之王");}} }7.集合綜合練習 1).站編號和站名對應關系如下:
將以上對應關系的數據存儲到map集合中,key:表示站編號,value:表示站名,并遍歷打印(可以不按順序打印):
2).計算地鐵票價規則:
3).打印格式(需要對鍵盤錄入的上車站和到達站進行判斷,如果沒有該站,提示重新輸入,直到站名存在為止):
public class Test6 {static Scanner scanner=new Scanner(System.in);/*** 站點輸入* @param map* @return*/public static String getName(Map<Integer,String> map){String name = scanner.nextLine();while(true){if(map.containsValue(name)){break;}System.out.println("該站不存在,請重新輸入:");name = scanner.nextLine();}return name;}/*** 計算站點數量* @param map* @param name* @return*/public static int getNum(Map<Integer,String> map,String name){//找numSet<Map.Entry<Integer, String>> entries = map.entrySet();for(Map.Entry<Integer, String> entry:entries){if(entry.getValue().equals(name)){return entry.getKey();}}return -1;}/*** 票價計算* @param count* @return*/public static double getMoney(int count){if(count<=3){return 3;}else if(count<=5){return 4;}else{if( (count-5)*2+4 >10){return 10;}return (count-5)*2+4;}}/*** 乘車時間* @param count* @return*/public static int getTime(int count){return count*2;}/*** 初始化站點* @return*/public static Map getMap(){Map<Integer,String> map= new HashMap<>();map.put(1,"朱辛莊");map.put(2,"育知路");map.put(3,"平西府");map.put(4,"回龍觀東大街");map.put(5,"霍營");map.put(6,"育新");map.put(10,"森林公園南門");map.put(12,"奧體中心");map.put(13,"北土城");return map;}/*** 售票結果* @param endNum* @param startNum* @param start* @param end*/public static void show(int endNum,int startNum,String start,String end){int count=Math.abs(endNum-startNum);double money=getMoney(count);int time=getTime(count);System.out.println("從"+start+"到"+end+"共經過"+count+"站收費"+money+"元,大約需要 "+time+"分鐘");}/*** 賣票系統入口*/public static void saleTicket(){Map<Integer,String> map=getMap();System.out.println("請輸入上車站:");String start=getName(map);int startNum=getNum(map,start);System.out.println("請輸入下車站:");String end=getName(map);int endNum=getNum(map,end);//計算show(endNum,startNum,start,end);}public static void main(String[] args) {saleTicket();} }總結
- 上一篇: 软件设计师教程笔记整理
- 下一篇: 系统分析师考试心得