HUAWEI華爲Java軟件工程師機試題——集合類
1、有如下Student對象,屬性如圖所示:
其中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) 計算各個班級的平均分
2、已知有16支足球隊參加2008北京奧運會。寫一個程序,隨機把16支球隊分成四組
3、設計Account對象如下:
要求完善設計,使得該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 和餘額。
4、已知某學校課程安排如下:
完成下列要求:
1) 使用一個Map,以老師的名字作爲鍵,以老師教授的課程名作爲值,表示上述
課程安排。
2) 增加了一位新老師Allen 教JDBC
3) Lucy 改爲教CoreJava
4) 遍歷Map,輸出所有的老師及老師教授的課程
5) *利用Map,輸出所有教JSP 的老師。
5、利用Map,完成下面的功能:
從命令行讀入一個字符串,表示一個年份,輸出該年的世界盃冠軍是哪支球隊。如果該
年沒有舉辦世界盃,則輸出:沒有舉辦世界盃。
6、在上題的基礎上繼續完成:輸入球隊的名字,輸出得冠的年份
如:輸入巴西,輸出1958 1962 1970 1994 2002
7、完成下面的要求
1) 創建一個List,在List 中增加三個工人,基本信息如下:
姓名 年齡 工資
zhang3 18 3000
li4 25 3500
wang5 22 3200
2) 在li4 之前插入一個工人,信息爲:姓名:zhao6,年齡:24,工資3300
3) 刪除wang5 的信息
4) 利用for 循環遍歷,打印List 中所有工人的信息
我的答案(HCQ,2018/8/7)
1.
package JulyTwelfthHUAWEI;
public class Student {
private String name;
private int age;
private double score;
private String classNum;
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 getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getClassNum() {
return classNum;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public Student(String name, int age, double score, String classNum) {
super();
this.name = name;
this.age = age;
this.score = score;
this.classNum = classNum;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;
import java.util.List;
import java.util.ArrayList;
public class StudentTest {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
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"));
double sumAge = 0;
double sumScore4 = 0;
double sumScore5 = 0;
double sumScore6 = 0;
double num4 = 0;
double num5 = 0;
double num6 = 0;
// 1)
for(Student stu : list) {
sumAge+=stu.getAge();
}
System.out.print("所有學生的平均年齡:");
System.out.println(sumAge/6.0);
// 2)
for(int i=0; i<list.size(); i++) {
if(list.get(i).getClassNum().equals("class04")) {
sumScore4+=list.get(i).getScore();
num4++;
}
if(list.get(i).getClassNum().equals("class05")) {
sumScore5+=list.get(i).getScore();
num5++;
}
if(list.get(i).getClassNum().equals("class06")) {
sumScore6+=list.get(i).getScore();
num6++;
}
}
System.out.print("class04的平均分數:");
System.out.println(sumScore4/num4);
System.out.print("class05的平均分數:");
System.out.println(sumScore5/num5);
System.out.print("class06的平均分數:");
System.out.println(sumScore6/num6);
}
}
2.
package JulyTwelfthHUAWEI;
import java.util.List;
public class Group {
private int id;
private String name;
private List<String> team;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getTeam() {
return team;
}
public void setTeam(List<String> team) {
this.team = team;
}
public Group(int id, String name, List<String> team) {
super();
this.id = id;
this.name = name;
this.team = team;
}
public Group() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Group [id=" + id + ", name=" + name + ", team=" + team + "]";
}
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Football {
public static void main(String[] args) {
List<String> teams = new ArrayList<String>();
Collections.addAll(teams,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P");
Collections.shuffle(teams);
System.out.println(teams);
Group[] groups = new Group[4];
for(int i = 0; i<groups.length; i++) {
groups[i] = new Group(i, "G"+i, new ArrayList<>());
}
int n = 0;
for(int i = 0;i < teams.size();i++){
groups[n++ % 4].getTeam().add(teams.get(i));
}
for(int i = 0; i<groups.length; i++) {
System.out.println(groups[i].getName()+groups[i].getTeam());
}
}
}
3.
package JulyTwelfthHUAWEI;
import java.util.Random;
public class Account {
private long id;
private double balance;
private String password;
Random rd = new Random();
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Account(double balance, String password) {
super();
this.id = Math.abs(rd.nextLong());
this.balance = balance;
this.password = password;
}
public Account() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + "]";
}
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AccountTest {
public static void main(String[] args) {
List<Account> list = new ArrayList<Account>();
list.add(new Account(10.00, "1234"));
list.add(new Account(15.00, "5678"));
list.add(new Account(0, "1010"));
Map<Long, Account> map = new HashMap<>();
for(Account account : list) {
map.put(account.getId(), account);
}
Collection<Account> c = map.values();
for (Account a : c) {
System.out.println(a.toString());
}
}
}
4.
package JulyTwelfthHUAWEI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Teacher {
public static void main(String[] args) {
// 1)
Map<String, String> map = new HashMap<>();
map.put("Tom", "CoreJava");//1001=jack
map.put("John", "Oracle");
map.put("Susan", "Oracle");
map.put("Jerry", "JDBC");
map.put("Kevin", "JSP");
map.put("Lucy", "JSP");
// 2)
map.put("Allen", "JDBC");
// 3)
map.replace("Lucy", "CoreJava");
// 4)
System.out.println(map);
// 5)
Set<Entry<String, String>> set = map.entrySet();
for(Entry<String, String> entry : set) {
if(entry.getValue().equals("JSP")) {
System.out.println(entry.getKey());
}
}
}
}
5. and 6.
package JulyTwelfthHUAWEI;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class WorldCup {
private static Scanner sc;
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("2006", "意大利");
map.put("2002", "巴西");
map.put("1998", "法國");
map.put("1994", "巴西");
map.put("1990", "德國");
map.put("1986", "阿根廷");
map.put("1982", "意大利");
map.put("1978", "阿根廷");
map.put("1974", "德國");
map.put("1970", "巴西");
map.put("1966", "英格蘭");
map.put("1962", "巴西");
map.put("1958", "巴西");
map.put("1954", "德國");
map.put("1950", "烏拉圭");
map.put("1938", "意大利");
map.put("1934", "意大利");
map.put("1930", "烏拉圭");
sc = new Scanner(System.in);
// 5.
System.out.print("請輸入年份:");
String year = sc.next();
if(map.containsKey(year)==true) {
System.out.print(year+"年的世界盃冠軍是:");
System.out.println(map.get(year));
} else {
System.out.println(year+"年沒有舉辦世界盃。");
}
// 6.
System.out.print("請輸入冠軍球隊的國家:");
String team = sc.next();
Set<Entry<String, String>> set = map.entrySet();
System.out.print(team+"隊獲得世界盃冠軍的年份是:");
for(Entry<String, String> entry : set) {
if(entry.getValue().equals(team)) {
System.out.print(entry.getKey()+" ");
}
}
}
}
7.
package JulyTwelfthHUAWEI;
public class Worker {
private String name;
private int age;
private int salary;
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 getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Worker(String name, int age, int salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public Worker() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Worker [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
**xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx**
package JulyTwelfthHUAWEI;
import java.util.ArrayList;
import java.util.List;
public class WorkerTest {
public static void main(String[] args) {
// 1)
List<Worker> list = new ArrayList<Worker>();
list.add(new Worker("zhang3", 18, 3000));
list.add(new Worker("li4", 25, 3500));
list.add(new Worker("wang5", 22, 3200));
// 2)
list.add(1, new Worker("zhao6", 24, 3300));
// 3)
for(int i=0; i<list.size(); i++) {
if(list.get(i).getName()=="wang5") {
list.remove(i);
}
}
// 4)
for(int i=0; i<list.size(); i++) {
System.out.println(list.get(i).toString());
}
}
}