一、选择题
- B
- A
- D
- AC
- A
- D
- C
- ABC
- C
- A
- C
二、编程题
1、
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List list = new ArrayList();
list.add("语文");
list.add("数学");
list.add("英语");
list.add("化学");
list.add("物理");
list.add("生物");
for (int i = 0; i < list.size(); i++) {
System.out.println("第" + (i + 1) + "个为" + list.get(i)) ;
}
}
}
2、
public class Employee {
private int id;
private String name;
private double salary;
public Employee() {
}
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
//测试类
import java.util.ArrayList;
import java.util.List;
public class EmployeeTest {
public static void main(String[] args) {
List list = new ArrayList();
Employee emp1 = new Employee(1, "张三", 5000);
Employee emp2 = new Employee(2, "李四", 5500);
Employee emp3 = new Employee(3, "赵六", 4000);
list.add(emp1);
list.add(emp2);
list.add(emp3);
System.out.printf("员工性别 ");
System.out.printf("员工薪资");
System.out.println();
for (Object employee:list) {
System.out.print(((Employee)employee).getName() + " ");
System.out.print(((Employee)employee).getSalary());
System.out.println();
}
}
}
3、
public class Student {
private int stuId;
private String name;
private float score;
public Student(int stuId, String name, float score) {
this.stuId = stuId;
this.name = name;
this.score = score;
}
public int getStuId() {
return stuId;
}
public void setStuId(int stuId) {
this.stuId = stuId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "[" +
"学号:" + stuId +
", 姓名: " + name + '\'' +
", 成绩: " + score +
']';
}
@Override
public boolean equals(Object o) {
if (this == o) return true; // 直接是一个对象,必然一样。
if (!(o instanceof Student)) return false; // 如果传入的不是学生类的对象,那么必然不是
Student student = (Student) o; // 将Object转换为Student
// 学号 & 姓名 是否相等
return getStuId() == student.getStuId() &&
Objects.equals(getName(), student.getName());
}
@Override
public int hashCode() {
return Objects.hash(getStuId(), getName());
}
}
//测试类
import java.util.HashSet;
import java.util.Iterator;
public class StudentTest {
public static void main(String[] args) {
Student student1 = new Student(3,"William",65.0f);
Student student2 = new Student(1,"Tom",87.0f);
Student student3 = new Student(2,"Lucy",95.0f);
HashSet hashSet = new HashSet();
hashSet.add(student1);
hashSet.add(student2);
hashSet.add(student3);
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
4、
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
//定义HashMap的对象并添加数据
// Scanner sc = new Scanner(System.in);
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(2006, "意大利");
map.put(2014, "德国");
map.put(2010, "西班牙");
Iterator it = map.values().iterator();
System.out.println("使用迭代器方式进行输出:");
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
//使用EntrySet同时获取key和value
Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
System.out.println("使用EntrySet进行输出:");
for(Map.Entry<Integer, String> entry : entrySet) {
System.out.print(entry.getKey() + "-");
System.out.println(entry.getValue());
}
}
}