容器作业1

1. 容器指的是“可以容纳其他对象的对象”,这种说法对吗?   dui

2. Collection/Set/List的联系跟区别?

Set和list实现collection接口,list不唯一 有序  set唯一 无序

3. Set和List的特点跟区别?

Set元素不能重复 linkedhashSet 根据添加顺序排序 treeSet根据自然顺序排序 没有索引  和collection的方法一样

List元素可以重复  list根据索引排序 arraylist按索引遍历效率高 但是增删效率低下移动元素 linkedList可以实现栈和队列的应用  linkedList增删效率高,反而遍历效率低下 和collection比添加了索引方法

4. 【上机】练习Collection接口中常用的方法

package TestCollection;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class TestCollection {

public static void main(String[] args) {

Collection<String> collection=new ArrayList<String>();

collection.add("占三");

collection.add("李四");

System.out.println(collection);

collection.remove(new String("占三"));

System.out.println(collection);

collection.add("123");

System.out.println(collection.contains("123"));

System.out.println(collection.isEmpty());

System.out.println(collection.size());

collection.add("haohao");

Iterator<String> elem=collection.iterator();

while(elem.hasNext()){

System.out.print(elem.next()+"  ");

}

}

}

5. 

6. 【上机】下面的代码,效果一致吗? 分析说明之。

Collection c = new HashSet();

Collection c2 = new HashSet();

Apple a = new Apple();

c.add(a);

c2.addAll(c);  

//增加另一个容器中所有的元素!       

Collection c = new HashSet();

Collection c2 = new HashSet();

Apple a = new Apple();

c.add(a);

c2.add(c);

不一致

后一个是将c对象作为元素整体添加在c2中

addAll()方法是将目标对象里边所有的元素添加到c2容器中

7. 想取两个容器中元素的交集,使用哪个方法?

Contains()

8. 说明isEmpty的作用,并说明下面代码有问题吗?

Collection c = null;

System.out.println(c.isEmpty());

判断引用类型是否为空,若空返回true不空返回false

空指针异常,有问题

9. 我想定义一个数组。该数组既可以放:Dog对象、也可以放Cat对象、还可以放
        Integer对象,怎么定义?

Collection<Object> c = new LinkedList<Object>();

c.add(new Integer(44));

c.add(new Dog());

c.add(new Cat());

 

 

List接口中增加了一些与顺序相关的操作方法,下面两个方法的作用是什么?

add(int index, E element) , get(int index)

add是在index索引处添加一个新元素element

get是获得index索引处的 元素

1. ArrayList底层使用什么来实现的? LinkedList是用什么实现的?

ArrayList底层是用顺序表数组来实现的,LinkedList是用链表来实现的

2. 说出ArrayLIst、LinkedList、Vector的区别。

arraylist  和 linkedlist   是线程不安全。vector 线程安全
arraylist  存储结构  连续的。
linkedlist  是链式存储。  数据结构不一致。

3. 我有一些数据,需要频繁的查询,插入和删除操作非常少,并且没有线程之间的共
        享,使用List下面的哪个实现类好一些?

ArrayList()方法好

4. 【上机】针对List中新增的有关顺序的方法,每个都进行测试。并且使用debug
         来帮助我们理解程序运行。

List<Integer> list=new ArrayList<Integer>();

list.add(44);

list.add(45);

list.add(23);

System.out.println(list);

list.add(1, 88);

System.out.println(list);

System.out.println(list.get(3));

List<Integer> list1=new ArrayList<Integer>();

list1.add(33);

list1.add(22);

list.addAll(list.size()-1,list1);

System.out.println(list);

System.out.println(list.indexOf(33));

5. 定义Computer类,使用价格排序。(使用Comparable接口)

package TestCollection;

public class Computer implements Comparable<Computer>{

private String name;

private int price;

private double size;

public Computer() {

super();

}

public Computer(String name, int price, double size) {

super();

this.name = name;

this.price = price;

this.size = size;

}

@Override

public int compareTo(Computer other) {

return this.price-other.price;

}

@Override

public String toString() {

return "Computer [name=" + name + ", price=" + price + ", size=" + size + "]";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = price;

}

public double getSize() {

return size;

}

public void setSize(double size) {

this.size = size;

}

}

 

 

6. package TestCollection;

7. 

8. import java.awt.Dialog;

9. import java.util.ArrayList;

10. import java.util.Collection;

11. import java.util.HashSet;

12. import java.util.LinkedList;

13. import java.util.List;

14. import java.util.Set;

15. import java.util.TreeSet;

16. 

17. public class TestCollection {

18. 

19. public static void main(String[] args) {

20. Set<Computer> set=new TreeSet<Computer>();

21. set.add(new Computer("dell",6999,47));

22. set.add(new Computer("lenovo",4380,46));

23. set.add(new Computer("lenovo",7000,47));

24. System.out.println(set);

25. }

26. 

27. }

28. equals返回true,hashcode一定相等吗? 

equals相等,则hashcode一定相等,反之则不然。

29. HashSet和TreeSet的区别

HashSet运行非常快 无序 唯一 底层实现的是哈希表

TreeSet自然顺序有序,底层实现的是二叉树

自定义类时hash需要重写equals和 hascode方法

TreeSet需要重写comparaTo方法

30. 使用HashSet存储自定义对象,为什么需要重写hashCode()和equals()?

在集合中,比如HashSet中,要求放入的对象不能重复,怎么判定呢?

首先会调用hashcode,如果hashcode相等,则继续调用equals,也相等,则认为重复。

如果重写equals后,如果不重写hashcode,则hashcode就是继承自Object的,返回内存编码,这时候可能出现equals相等,而hashcode不等,你的对象使用集合时,就等不到正确的结果

 

31. 使用TreeSet存储多个学生数据,实现按照不同属性值进行排序?

package cn.sxt.set;

public class Student implements Comparable<Student>{

private int id;

private String name;

private int age;

private double  score;

public Student() {

super();

}

public Student(int id, String name, int age, double score) {

super();

this.id = id;

this.name = name;

this.age = age;

this.score = score;

}

@Override

public int compareTo(Student other) {

return name.compareTo(other.name);

}

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 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;

}

@Override

public String toString() {

return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]";

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + age;

result = prime * result + id;

result = prime * result + ((name == null) ? 0 : name.hashCode());

long temp;

temp = Double.doubleToLongBits(score);

result = prime * result + (int) (temp ^ (temp >>> 32));

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Student other = (Student) obj;

if (age != other.age)

return false;

if (id != other.id)

return false;

if (name == null) {

if (other.name != null)

return false;

} else if (!name.equals(other.name))

return false;

if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))

return false;

return true;

}

}

32. package cn.sxt.set;

33. 

34. import java.util.HashSet;

35. import java.util.LinkedHashSet;

36. import java.util.Set;

37. import java.util.TreeSet;

38. 

39. public class TestStudent {

40. 

41. public static void main(String[] args) {

42. 

43. Set<Student> set=new TreeSet<Student>();

44. set.add(new Student(1,"高雷",18,95.3));

45. set.add(new Student(2,"郑云",20,95.7));

46. set.add(new Student(3,"二雷",23,90));

47. set.add(new Student(3,"二雷雷",23,90));

48. System.out.println(set.size());

49. System.out.println(set);

50. 

51. Set<Student> set1=new TreeSet<Student>(new ScoreComparator());

52. set1.add(new Student(1,"高雷",18,95.3));

53. set1.add(new Student(2,"郑云",20,95.7));

54. set1.add(new Student(3,"二雷",23,90));

55. set1.add(new Student(3,"二雷雷",23,90));

56. System.out.println(set1.size());

57. System.out.println(set1);

58. 

59. Set<Student> set3=new TreeSet<Student>(new NameComparator(){

60. 

61. @Override

62. public int compare(Student stu1, Student stu2) {

63. if (stu1.getName()!=stu2.getName()) {

64. return stu1.getName().compareTo(stu2.getName());

65. }else{

66. return stu1.getId()-stu2.getId();

67. }

68. }

69. 

70. });

71. set3.add(new Student(1,"aaa",18,95.3));

72. set3.add(new Student(2,"bbb",20,95.7));

73. set3.add(new Student(3,"ccc",23,90));

74. set3.add(new Student(4,"ddd",23,90));

75. set3.add(new Student(5,"ddd",23,90));

76. 

77. System.out.println(set3.size());

78. System.out.println(set3);

79. }

80. 

81. }

82. 【上机】说明Comparable接口作用。并定义一个学生类,使用分数来比较大小。

子类实现该接口重写compareTo()方法,进行自己规定的大小比较规则

public class Student{

private int id;

private String name;

private double score;

Student(){}

public Student(int id, String name,double score) {

super();

this.id = id;

this.name = name;

this.score = score;

}

@Override

public String toString() {

return "Computer [name=" + name + ", price=" + price + ", size=" + size + "]";

}

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 getScore() {

return score;

}

public void setScore(double score) {

this.score = score;

}

}

Public static viod main(String[] args){

Set set=new TreeSet();

Set.add(new  Student(1,“高磊”,98));

Set.add(new  Student(2,“李四”,94));

System.out.println(Arrays.toString(set));

}

猜你喜欢

转载自blog.csdn.net/qq_36421955/article/details/80823918
今日推荐