您的位置:首页 > 房产 > 建筑 > 焦作市网站建设_温州龙湾区新冠疫情最新_米拓建站_欧美网站建设

焦作市网站建设_温州龙湾区新冠疫情最新_米拓建站_欧美网站建设

2024/12/22 2:44:56 来源:https://blog.csdn.net/weixin_42242910/article/details/144375026  浏览:    关键词:焦作市网站建设_温州龙湾区新冠疫情最新_米拓建站_欧美网站建设
焦作市网站建设_温州龙湾区新冠疫情最新_米拓建站_欧美网站建设

1.展示多部电影信息

 新建一个实体类Movie

package com.test.集合框架.p137;
public class Movie {private String name;//电影名称private double score;//电影评分private String actor;//电影主演public Movie() {}public Movie(String name, double score, String actor) {this.name = name;this.score = score;this.actor = actor;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}public String getActor() {return actor;}public void setActor(String actor) {this.actor = actor;}@Overridepublic String toString() {return "Movie{" +"name='" + name + '\'' +", score=" + score +", actor='" + actor + '\'' +'}';}
}

新建一个MovieTest类

package com.test.集合框架.p137;
import java.util.ArrayList;
import java.util.Collection;
/*** 目标:完成电影信息的展示。*         new Movie("《肖生克的救赎》" , 9.7 ,  "罗宾斯")*         new Movie("《霸王别姬》" , 9.6 ,  "张国荣、张丰毅")*         new Movie("《阿甘正传》" , 9.5 ,  "汤姆.汉克斯")*/
public class MovieTest {public static void main(String[] args) {// 1、创建一个集合容器负责存储多部电影对象。Collection<Movie> movies = new ArrayList<>();movies.add( new Movie("《肖生克的救赎》" , 9.7 ,  "罗宾斯"));movies.add( new Movie("《霸王别姬》" , 9.6 ,  "张国荣、张丰毅"));movies.add( new Movie("《阿甘正传》" , 9.5 ,  "汤姆.汉克斯"));System.out.println(movies);for (Movie movie : movies) {System.out.println("电影名:" + movie.getName());System.out.println("评分:" + movie.getScore());System.out.println("主演:" + movie.getActor());System.out.println("---------------------------------------------");}}
}

运行结果:

2.两个内容一样的对象,让HashSet集合去重

自定义的类型的对象,比如两个内容一样的学生对象,如果让HashSet集合能够去重复!

 现象举例:

新建一个学生类

package com.test.集合框架.p140;
public class Student {private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}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 double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}
}

再创建一个HashSetTest类

package com.test.集合框架.p140;
import java.util.HashSet;
import java.util.Set;
/*** 目标:自定义的类型的对象,比如两个内容一样的学生对象,如果让HashSet集合能够去重复!*/
public class HashSetTest {public static void main(String[] args) {Set<Student> students = new HashSet<>();Student s1 = new Student("至尊宝", 28, 169.6);Student s2 = new Student("蜘蛛精", 23, 169.6);Student s3 = new Student("蜘蛛精", 23, 169.6);Student s4 = new Student("牛魔王", 48, 169.6);students.add(s1);students.add(s2);students.add(s3);students.add(s4);System.out.println(students);}
}

结论:如果希望Set集合认为2个内容一样的对象是重复的, 必须重写对象的hashCode()和equals()方法

修改学生类-重写hashCode()和equals()方法

再运行HashSetTest类-就没有两个“蜘蛛精”对象了

 3.斗地主游戏

分析业务需求

  • 总共有54张牌
  • 点数: "3","4","5","6","7","8","9","10","J","Q","K","A","2“
  • 花色: "♠", "♥", "♣", "♦“
  • 大小王: "👲" , "🃏“
  • 斗地主:发出51张牌,剩下3张做为底牌。

分析实现

  • 在启动游戏房间的时候,应该提前准备好54张牌
  • 接着,需要完成洗牌、发牌、对牌排序、看牌

新建Card类(牌)

package com.test.集合框架.p144;
/*** @description:牌类* @author: Admin* @create: 2024/9/27 9:30*/
public class Card {private String number;//点数private String color;//牌的花色private int size;//每张牌是存在大小的public Card() {}public Card(String number, String color, int size) {this.number = number;this.color = color;this.size = size;}public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}@Overridepublic String toString() {return color+number+"-->"+size;}
}

 新建房间类Room

package com.test.集合框架.p144;import java.util.*;/*** @description:房间类* @author: Admin* @create: 2024/9/27 9:32*/
public class Room {//必须有一副牌private List<Card> allCards=new ArrayList<>();public Room() {//点数:一个数确定了,类型确定String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};//花色String[] colors={"♠", "♥", "♣", "♦"};int size=0;//表示每张牌的大小//点色与花色组合起来for (String number : numbers) {size++;for (String color : colors) {Card c=new Card(number,color,size);allCards.add(c);}}//单独存入大小王Card c1 = new Card("",  "🃏" , ++size);Card c2 = new Card("", "👲" , ++size);Collections.addAll(allCards,c1,c2);System.out.println("这是一副新牌:"+allCards);}//游戏启动方法public void start() {//1.洗牌Collections.shuffle(allCards);System.out.println("洗牌后:"+allCards);//2发牌List<Card> LinHuChong=new ArrayList<>();List<Card> JiuGuoZhi=new ArrayList<>();List<Card> RenYingYing=new ArrayList<>();// 正式发牌给这三个玩家,依次发出51张牌,剩余3张做为底牌。// allCards = [♥3, ♣10, ♣4, ♥K, ♦Q, ♣2, 🃏, ♣8, ....//             0     1   2   3   4   5   6 ...   % 3for(int i=0;i<allCards.size()-3;i++){Card c=allCards.get(i);//判断牌发给谁if(i%3==0){//给LinHuChong发牌LinHuChong.add(c);} else if (i%3==1) {//给JiuGuoZhi发牌JiuGuoZhi.add(c);} else if (i%3==2) {//给Renyingying发牌RenYingYing.add(c);}}//对3个玩家的牌排序sortCards(LinHuChong);sortCards(JiuGuoZhi);sortCards(RenYingYing);//4.看牌System.out.println("啊冲:"+LinHuChong);System.out.println("啊智:"+JiuGuoZhi);System.out.println("茵茵:"+RenYingYing);List<Card> lastThreadCards=allCards.subList(allCards.size()-3,allCards.size());System.out.println("最后三张底牌:"+lastThreadCards);//假设JiuGuoZhi抢到地主JiuGuoZhi.addAll(lastThreadCards);sortCards(JiuGuoZhi);System.out.println("啊智抢到地主后的牌:"+JiuGuoZhi);}private void sortCards(List<Card> cards){Collections.sort(cards, new Comparator<Card>() {@Overridepublic int compare(Card o1, Card o2) {return o2.getSize()- o1.getSize();//降序}});}
}

新建Demo类

package com.test.集合框架.p144;/*** @description* @author: Admin* @create: 2024/9/27 9:56*/
public class Demo {public static void main(String[] args) {Room r=new Room();r.start();}
}

运行:

4.统计投票人数 -Map集合

分析:

  • 将80个学生选择的数据拿到程序中去,[A, A, B , A, B,  C, D, ...]
  • 准备一个Map集合用于存储统计的结果,Map<String,Integer>,键是景点,值代表投票数量。
  • 遍历80个学生选择的景点,每遍历一个景点,就看Map集合中是否存在该景点,不存在存入“景点=1“,存在则其对应值+1
package com.test.集合框架;
import java.util.*;
/*** @description* @author: Admin* @create: 2024/9/23 14:55*/
public class p146 {public static void main(String[] args) {//建一个list集合,存放80位选择的景点信息List<String> all=new ArrayList<>();//数组存放景点String[] view={"A","B","C","D"};//生成一个随机数Random r =new Random();for (int i=1;i<=80;i++){int number=r.nextInt(0,4);//包前不包后 0 1 2 3all.add(view[number]);}//生成80个景点System.out.println("所有景点信息:"+all);//统计Map<String,Integer> result=new HashMap<>();for (String s : all) {if(result.containsKey(s)){//包含该键,值就得+1result.put(s,result.get(s)+1);}else {//不包含该键,值设为1---第一次有这个键result.put(s,1);}}System.out.println(result);}
}

 运行

5.记录省份以及对应的城市信息-Map集合案例(嵌套的集合)

 

package com.test.集合框架;import java.util.*;public class p148 {public static void main(String[] args) {// 1、定义一个Map集合存储全部的省份信息,和其对应的城市信息。Map<String, List<String>> map = new HashMap<>();List<String> cities1 = new ArrayList<>();Collections.addAll(cities1, "南京市","扬州市","苏州市" ,"无锡市","常州市");map.put("江苏省", cities1);List<String> cities2 = new ArrayList<>();Collections.addAll(cities2, "武汉市","孝感市","十堰市","宜昌市","鄂州市");map.put("湖北省", cities2);List<String> cities3 = new ArrayList<>();Collections.addAll(cities3, "石家庄市","唐山市", "邢台市", "保定市", "张家口市");map.put("河北省", cities3);System.out.println(map);List<String> cities = map.get("湖北省");for (String city : cities) {System.out.println(city);}map.forEach((p, c) -> {System.out.println(p + "----->" + c);});}
}

 运行:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com