java集合练习题
1.使用List集合存儲(chǔ)10個(gè)學(xué)生信息。
學(xué)生信息:姓名,年齡,成績(jī)。
統(tǒng)計(jì)所有姓“張”的同學(xué)的平均成績(jī)。
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("來(lái)三",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("姓張的平均成績(jī)?yōu)?#xff1a;"+sum/b);} }?2.產(chǎn)生10個(gè)1-100的隨機(jī)數(shù),并放到一個(gè)數(shù)組中,把數(shù)組中大于等于10的數(shù)字放到一個(gè)list集合中,并打印到控制臺(tái)。
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個(gè)數(shù)組,第一個(gè)數(shù)組內(nèi)容為:[黑龍江省,浙江省,江西省,廣東省,福建省],第二個(gè)數(shù)組為:[哈爾濱,杭州,南昌,廣州,福州],將第一個(gè)數(shù)組元素作為key,第二個(gè)數(shù)組元素作為value存儲(chǔ)到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” 去掉重復(fù)元素
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.定義一個(gè)泛型為String類(lèi)型的List集合,統(tǒng)計(jì)該集合中每個(gè)字符(注意,不是字符串)出現(xiàn)的次數(shù)。例如:集合中有”abc”、”bcd”兩個(gè)元素,程序最終輸出結(jié)果為:“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)從命令行讀入一個(gè)字符串,表示一個(gè)年份,輸出該年的世界杯冠軍是哪支球隊(duì)。如果該 年沒(méi)有舉辦世界杯,則輸出:沒(méi)有舉辦世界杯。
(2)在原有世界杯Map 的基礎(chǔ)上,增加如下功能: 讀入一支球隊(duì)的名字,輸出該球隊(duì)奪冠的年份列表。 例如,讀入“巴西”,應(yīng)當(dāng)輸出 1958 1962 1970 1994 2002 讀入“荷蘭”,應(yīng)當(dāng)輸出 沒(méi)有獲得過(guò)世界杯
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,"法國(guó)","意大利");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,"美國(guó)","巴西");MapDemo mapDemo16=new MapDemo(16,1998,"法國(guó)","法國(guó) ");MapDemo mapDemo17=new MapDemo(17,2002,"韓日","巴西");MapDemo mapDemo18=new MapDemo(18,2006,"德國(guó)","意大利");MapDemo mapDemo19=new MapDemo(19,2010,"南非","西班牙");MapDemo mapDemo20=new MapDemo(20,2014,"巴西","德國(guó)");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、根據(jù)年份找冠軍Scanner scanner=new Scanner(System.in);int y = scanner.nextInt();MapDemo mapDemo = map.get(y);if(mapDemo==null){System.out.println("該年沒(méi)有舉辦世界杯");}else{System.out.println("冠軍是:"+mapDemo.getTeam());}// 2\根據(jù)冠軍找年份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("無(wú)冕之王");}} }7.集合綜合練習(xí) 1).站編號(hào)和站名對(duì)應(yīng)關(guān)系如下:
將以上對(duì)應(yīng)關(guān)系的數(shù)據(jù)存儲(chǔ)到map集合中,key:表示站編號(hào),value:表示站名,并遍歷打印(可以不按順序打印):
2).計(jì)算地鐵票價(jià)規(guī)則:
3).打印格式(需要對(duì)鍵盤(pán)錄入的上車(chē)站和到達(dá)站進(jìn)行判斷,如果沒(méi)有該站,提示重新輸入,直到站名存在為止):
public class Test6 {static Scanner scanner=new Scanner(System.in);/*** 站點(diǎn)輸入* @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("該站不存在,請(qǐng)重新輸入:");name = scanner.nextLine();}return name;}/*** 計(jì)算站點(diǎn)數(shù)量* @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;}/*** 票價(jià)計(jì)算* @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;}}/*** 乘車(chē)時(shí)間* @param count* @return*/public static int getTime(int count){return count*2;}/*** 初始化站點(diǎn)* @return*/public static Map getMap(){Map<Integer,String> map= new HashMap<>();map.put(1,"朱辛莊");map.put(2,"育知路");map.put(3,"平西府");map.put(4,"回龍觀(guān)東大街");map.put(5,"霍營(yíng)");map.put(6,"育新");map.put(10,"森林公園南門(mén)");map.put(12,"奧體中心");map.put(13,"北土城");return map;}/*** 售票結(jié)果* @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+"共經(jīng)過(guò)"+count+"站收費(fèi)"+money+"元,大約需要 "+time+"分鐘");}/*** 賣(mài)票系統(tǒng)入口*/public static void saleTicket(){Map<Integer,String> map=getMap();System.out.println("請(qǐng)輸入上車(chē)站:");String start=getName(map);int startNum=getNum(map,start);System.out.println("請(qǐng)輸入下車(chē)站:");String end=getName(map);int endNum=getNum(map,end);//計(jì)算show(endNum,startNum,start,end);}public static void main(String[] args) {saleTicket();} }總結(jié)
- 上一篇: 软件设计师教程笔记整理
- 下一篇: 人口logistic模型公式_数学建模l