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;
}
@Override
public 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 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 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 map=new HashMap<>();
for (int i = 0; i < shengs.length; i++) {
map.put(shengs[i],shis[i]);
}
Set 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 list=new ArrayList<>();
list.add("aa");
list.add("aa");
list.add("bb");
list.add("cc");
list.add("bb");
// Set set=new HashSet<>();
// for (String s:list){
// set.add(s);
// }
Set 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 list=new ArrayList<>();
list.add("ahkjfha");
list.add("afdadfg");
Map 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 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 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 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 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 map,String name){
//找num
Set> entries = map.entrySet();
for(Map.Entry 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 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 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();
}
}