經典查單詞
統計一段文章中每個單詞出現的次數
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());
}
}