千锋逆战班学习第二十五天

千锋逆战班学习第25天
努力或许没有收获,但不努力一定没收获,加油。
今天我学了Java课程的集合。
中国加油!!!武汉加油!!!千锋加油!!!我自己加油!!!

4.有如下代码:

public class Test4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List list=new ArrayList();
		list.add("Hello");
		list.add("Lean");
		list.add("Hello");
		list.add("Welcome");
		Set set=new HashSet();
		set.hashCode();
		set.addAll(list);
		System.out.println(set.size());
	}

}
3

6.为第5题的worker类,在添加完equals方法的基础上,添加一个hashCode方法

public int hashCode(){
//1
}
(1)	return 0;
(2)	int result = 0;
if(name!=null) result = name.hashCode();
return result + age;
(3)	return super.hashCode();
现在要把Worker类放入HashSet中,并希望HashSet中没有重复元素,则
A.三种写法都正确
B. |、II写法正确, II的效率更高
C.II写法正确,1、III写法都不正确

b

7.代码改错

class Worker {
	String name;
	int age;
	double salary;

	public Worker() {
	}

	public Worker(String name, int age, double salary) {
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	int hashCode() {
		return name.hashCode() + age + salary;
	}

	public boolean equals(Worker w) {
		if (w.name == name && w.salary == salary && w.age == age) {
			return true;
		} else {
			return false;
		}
	}
}

public class TestWorker {
	public static void main(String[] args) {
		Set set = new HashSet();
		set.add(new Worker("tom", 18, 2000));
		set.add(new Worker("tom", 18, 2000));
		set.add(0,new Worker("jerry", 18, 2000));
		System.out.println(set.size());
	}
}
//1 重写父类方法,权限不能小于父类;salary是double类型,返回值要强转为int型
public int hashCode() {
		return (int) (name.hashCode() + age + salary);
	} 
//2 HashSet是无序的集合,不能在指定位置添加
set.add(new Worker("jerry", 18, 2000));

8.在前面Worker类的基础上,为Worker类添加相应的方法,使得Worker放入HashSet中时,Set中没有重复元素。并编写相应的测试代码。

重写equals方法
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Worker other = (Worker) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
			return false;
		return true;
	}
	public class TestWorker {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Set set=new HashSet();
		set.add(new Worker("tom",18,2000));
		set.add(new Worker("tom",18,2000));
		set.add(new Worker("jerry",18,2000));
		System.out.print(set.size());
		System.out.print(set.toString());
	}

}

执行结果2[Test10.edcs10.Worker@fb467c93, Test10.edcs10.Worker@40d660df]

10.利用Map完成以下功能 从命令行读取一个字符串,表示一个年份,输出该年世界杯冠军是哪支球队。如果该年没有举办世界杯,则输出:没有举办世界杯

public class Test10 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map 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", "乌拉圭");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入世界杯冠军的年份");
		String s = sc.nextLine();
		if (map.containsKey(s) == false) {
			System.out.println("该年没有举办世界杯");
		} else {
			System.out.println("该年的世界杯冠军是" + map.get(s));
		}
	}
}
请输入世界杯冠军的年份
1998
该年的世界杯冠军是法国

11.

已知某学校课程安排
(1)使用Map,以老师名字作为键,教授的课程名作为值,表示上述课程安排
(2)增加一位新教师Allen教JDBC
(3)Lucy改为教CoreJava
(4)遍历Map,输出所有老师及老师教授的课程
(5)利用Map,输出所有教JSP的老师

public class Test11 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		map.put("Tom", "CoreJava");
		map.put("John", "Oracle");
		map.put("Susan", "Oracle");
		map.put("Jerry", "JDBC");
		map.put("Jim", "Unix");
		map.put("Kevin", "JSP");
		map.put("Lucy", "JSP");
		map.put("Allen", "JDBC");
		map.replace("Lucy", "CoreJava");
		Set s=map.keySet();
		System.out.println(map.toString());
		for(String i:s) {
			if(map.get(i).equals("JSP")) {
				System.out.println(i);
			}
		}
	}

}

{Kevin=JSP, Tom=CoreJava, Susan=Oracle, John=Oracle, Lucy=CoreJava, Jerry=JDBC, Allen=JDBC, Jim=Unix}
Kevin

12.有如下代码:

class Student {
	String name;
	int age;

	public Student() {
	}

	public Student(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public int hashCode() {
		return name.hashCode() + age;
	}

	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (o == this)
			return true;
		if (o.getClass() != this.getClass())
			return false;
		Student stu = (Student) o;
		if (stu.name.equals(name) && stu.age == age)
			return true;
		else
			return false;
	}
}

public class TestHashSet {
	public static void main(String[] args) {
		Set set = new HashSet();
		Student stu1 = new Student();
		Student stu2 = new Student("Tom", 18);
		Student stu3 = new Student("Tom", 18);
		set.add(stu1);
		set.add(stu2);
		set.add(stu3);
		System.out.println(set.size());
	}
}
运行结果:

编译正确,运行时异常,空指针异常

13.在原有世界杯Map的基础上,增加如下功能
读入一支球队的名字,输出该球队夺冠的年份列表

Scanner input = new Scanner(System.in);
		System.out.print("输入一个球队:");
		String str = input.nextLine();
		if (map.containsValue(str)) {
			for (String s : map.keySet()) {
				if (map.get(s).contains(str)) {
					System.out.println(s + "\t");
				}
			}
		} else {
			System.out.println("该球队没有获得过世界杯");
		}
	}
}
运行结果:

输入一个球队:巴西
1962	
1994	
1970	
2002	
1958

输入一个球队:中国
该球队没有获得过世界杯

14.给定一个字符串,输出该字符串由哪些字符组成,每个字符出现多少次

public class Test15 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		Map count = new HashMap();
		char[] chars = s.toCharArray();
		for (char ch : chars) {
			if (count.containsKey(ch)==false) {
				count.put(ch, 0);
			}
			int sum = count.get(ch);
			count.put(ch, sum+ 1);
		}
		Set keys = count.keySet();
		for (Character ch : keys) {
			System.out.println("字符" + ch + "出现次数:" + count.get(ch));
		}
	}

}

fhosdvojpa
字符p出现次数:1
字符a出现次数:1
字符s出现次数:1
字符d出现次数:1
字符f出现次数:1
字符v出现次数:1
字符h出现次数:1
字符j出现次数:1
字符o出现次数:2

你可能感兴趣的:(java)