经典查单词
统计一段文章中每个单词出现的次数
public static void main(String[] args) {
String speak = "this is a book this is an elephont";
String[] words = speak.split(" ");
Map<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++) {
if (map.containsKey(words[i])) {
int count = map.get(words[i]);
count++;
map.put(words[i], count);
} else {
map.put(words[i], 1);
}
}
for (String string : map.keySet()) {
System.out.println(string + "---------" + map.get(string));
}
}
1.第一题 (Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。
历届世界杯冠军
届数 举办年份 举办地点 冠军
第一届 1930年 乌拉圭 乌拉圭
第二届 1934年 意大利 意大利
第三届 1938年 法国 意大利
第四届 1950年 巴西 乌拉圭
第五届 1954年 瑞士 西德
第六届 1958年 瑞典 巴西
第七届 1962年 智利 巴西
第八届 1966年 英格兰 英格兰
第九届 1970年 墨西哥 巴西
第十届 1974年 前西德 西德
第十一届 1978年 阿根廷 阿根廷
第十二届 1982年 西班牙 意大利
第十三届 1986年 墨西哥 阿根廷
第十四届 1990年 意大利 西德
第十五届 1994年 美国 巴西
第十六届 1998年 法国 法国
第十七届 2002年 韩日 巴西
第十八届 2006年 德国 意大利
第十九届 2010年 南非 西班牙
第二十届 2014年 巴西 德国
(Map)在原有世界杯Map 的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。 例如,读入“巴西”,应当输出 1958 1962 1970 1994 2002 读入“荷兰”,应当输出 没有获得过世界杯
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1930, "乌拉圭");
map.put(1934, "意大利");
map.put(1938, "意大利");
map.put(1950, "乌拉圭");
map.put(1954, "西德");
map.put(1958, "巴西");
map.put(1962, "巴西");
map.put(1966, "英格兰");
map.put(1970, "巴西");
map.put(1974, "西德");
map.put(1978, "阿根廷");
map.put(1982, "意大利");
map.put(1986, "阿根廷");
map.put(1990, "西德");
map.put(1994, "巴西");
map.put(1998, "法国");
map.put(2002, "巴西");
map.put(2006, "意大利");
map.put(2010, "西班牙");
map.put(2014, "德国");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("输入年份");
int year = scanner.nextInt();
if (map.containsKey(year)) {
System.out.println("该年的冠军是" + map.get(year));
} else {
System.out.println("该年没有举办世界杯");
break;
}
}
System.out.println("输入国家");
String country = scanner.next();
boolean i = false;
for (Integer year : map.keySet()) {
if (country.equals(map.get(year))) {
// System.out.println("该队获得冠军的年份是");
System.out.println(year);
i = true;
}
}
if (i == false) {
System.out.println("该国家太弱了");
}
}
2.第二题 已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数
2008 北京奥运会男足参赛国家:
科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利
提示:分配一个,删除一个
public static void main(String[] args) {
Map<Integer, List<String>> Group2Countrys = new HashMap<Integer, List<String>>();
String s = "科特迪瓦,阿根廷,澳大利亚,塞尔维亚,荷兰,尼日利亚,日本,美国,中国,新西兰,巴西,比利时,韩国,喀麦隆,洪都拉斯,意大利";
String[] country = s.split(",");
List<String> wordcupgroup = new ArrayList<String>();
for (int i = 0; i < country.length; i++) {
wordcupgroup.add(country[i]);
}
//1组
for (int j = 0; j < 4; j++) {
List<String> wordcupgroup1 = new ArrayList<String>();
for (int i = 0; i < 4; i++) {
Random random = new Random();
int index = random.nextInt(wordcupgroup.size());
wordcupgroup1.add(wordcupgroup.get(index));
wordcupgroup.remove(wordcupgroup.get(index));
}
Group2Countrys.put(j + 1, wordcupgroup1);
}
for (Integer bl : Group2Countrys.keySet()) {
System.out.println("第" + bl + "组");
List<String> con = Group2Countrys.get(bl);
for (String s1 : con) {
System.out.print(s1 + "\t");
}
System.out.println();
}
}
3.第三题 有如下Student 对象,姓名,年龄,成绩,班级号,
private String name;
private int age;
private int score;
private String classNum;
其中,classNum 表示学生的班号,例如“class05”。 有如下
List List list = new ArrayList();
list.add(new Student(“Tom”, 18, 100, “class05”));
list.add(new Student(“Jerry”, 22, 70, “class04”));
list.add(new Student(“Owen”, 25, 90, “class05”));
list.add(new Student(“Jim”, 30,80 , “class05”));
list.add(new Student(“Steve”, 28, 66, “class06”));
list.add(new Student(“Kevin”, 24, 100, “class04”));
在这个list 的基础上,完成下列要求:
1) 计算所有学生的平均年龄
2) 计算各个班级的平均分
public class Student {
private String name;
private int age;
private double socre;
private String ClassNum;
public Student(String name, int age, double socre, String ClassNum) {
this.name = name;
this.age = age;
this.ClassNum = ClassNum;
this.socre = socre;
}
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 getSocre() {
return socre;
}
public void setSocre(double socre) {
this.socre = socre;
}
public String getClassNum() {
return ClassNum;
}
public void setClassNum(String classNum) {
ClassNum = classNum;
}
}
public class ListStudent {
public static void main(String[] args) {
List<Student> StudentScore = new ArrayList<Student>();
StudentScore.add(new Student("aa", 13, 89.5, "01"));
StudentScore.add(new Student("bb", 14, 89, "02"));
StudentScore.add(new Student("cc", 12, 90, "03"));
StudentScore.add(new Student("dd", 14, 79, "01"));
StudentScore.add(new Student("ee", 13, 100, "02"));
StudentScore.add(new Student("ff", 14, 93, "03"));
int totle = 0;
for (Student student : StudentScore) {
totle += student.getAge();
}
System.out.println("平均年龄是" + totle / StudentScore.size() + "岁");
Map<String, List<Double>> Class2avgscore = new HashMap<String, List<Double>>();
for (Student student : StudentScore) {
String classname = student.getClassNum();
double score = student.getSocre();
if (Class2avgscore.containsKey(classname)) {
List<Double> everyclass = Class2avgscore.get(classname);
everyclass.add(score);
} else {
List<Double> everyclass = new ArrayList<>();
everyclass.add(score);
Class2avgscore.put(classname, everyclass);
}
}
for (String string : Class2avgscore.keySet()) {
double totlescore = 0;
System.out.println(string + "班");
List<Double> avgs = Class2avgscore.get(string);
for (Double scores : avgs) {
totlescore += scores;
}
System.out.println("平均成绩是" + totlescore / avgs.size());
}
}
}
第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。
public static void reverseList(List<String> stringList)
{
//新建一个list
List<String> stringList1 = new ArrayList<>();
int index2 = 1;
for(String string : stringList)
{
//得到倒数第index2个元素
String strLast = stringList.get(stringList.size() - index2);
//加到新的list
stringList1.add(strLast);
index2++;
}
int index3 = 0;
for(String string : stringList1)
{
stringList.set(index3, string);
index3++;
}
}
public static void printList(List<String> stringList)
{
int index = 1;
for(String string : stringList)
{
System.out.print("第" + index + "个元素:");
System.out.println(string);
index++;
}
}
public static void main(String[] args) {
List<String> stringList = new ArrayList<>();
stringList.add("zhangsan");
stringList.add("lisi");
stringList.add("wangwu");
stringList.add("liuliu");
System.out.println("反转之前");
printList(stringList);
reverseList(stringList);
// Collections.reverse(stringList);
// Collections.shuffle(stringList);//随机打乱list集合中的数据
System.out.println("反转之后");
printList(stringList);
}
5.第五题(Map)设计Account 对象如下:
private long id;
private double balance;
private String password;
要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下:
List list = new ArrayList();
list.add(new Account(10.00, “1234”));
list.add(new Account(15.00, “5678”));
list.add(new Account(0, “1010”));
要求把List 中的内容放到一个Map 中,该Map 的键为id,值为相应的Account 对象。 最后遍历这个Map,打印所有Account 对象的id 和余额。
用UUID的方法
public class Account {
private String id;
private double balabce;
private String powerword;
public Account(double balabce, String powerword) {
this.balabce = balabce;
this.powerword = powerword;
this.id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getBalabce() {
return balabce;
}
public void setBalabce(double balabce) {
this.balabce = balabce;
}
public String getPowerword() {
return powerword;
}
public void setPowerword(String powerword) {
this.powerword = powerword;
}
}
public static void main(String[] args) {
List<Account> accountList = new ArrayList<Account>();
accountList.add(new Account(159, "aaa"));
accountList.add(new Account(187, "hhh"));
accountList.add(new Account(188, "hdd"));
accountList.add(new Account(155, "hff"));
accountList.add(new Account(170, "ggg"));
accountList.add(new Account(150, "htt"));
Map<String, Account> Id2Balance = new HashMap<String, Account>();
for (Account account : accountList) {
Id2Balance.put(account.getId(), account);
}
for (Map.Entry<String, Account> entry : Id2Balance.entrySet()) {
System.out.println("id=" + entry.getKey() + "余额" + entry.getValue().getBalabce());
}
}
随机生成的id
public class Account {
private long id;
private double balabce;
private String powerword;
private static int object_count=0;//多加的
public Account(double balabce, String powerword) {
this.balabce = balabce;
this.powerword = powerword;
this.id= Account.object_count+1;//调用
Account.object_count++;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getBalabce() {
return balabce;
}
public void setBalabce(double balabce) {
this.balabce = balabce;
}
public String getPowerword() {
return powerword;
}
public void setPowerword(String powerword) {
this.powerword = powerword;
}
}
public static void main(String[] args) {
List<Account> accountList = new ArrayList<Account>();
accountList.add(new Account(159, "aaa"));
accountList.add(new Account(187, "hhh"));
accountList.add(new Account(188, "hdd"));
accountList.add(new Account(155, "hff"));
accountList.add(new Account(170, "ggg"));
accountList.add(new Account(150, "htt"));
Map<Long, Account> Id2Balance = new HashMap<Long, Account>();//Key 变
for (Account account : accountList) {
Id2Balance.put(account.getId(), account);
}
for (Map.Entry<Long, Account> entry : Id2Balance.entrySet()) //Key 变
{
System.out.println("id=" + entry.getKey() + "余额" + entry.getValue().getBalabce());
}
}