java0526作业

一、选择题

  1. B
  2. A
  3. D
  4. AC
  5. A
  6. D
  7. C
  8. ABC
  9. C
  10. A
  11. 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());
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值