前言
这是我从网上找来的习题,自己看着题目写了点自己认为的答案,有可能不太好,以后有时间再来改正吧。
目录
题目1
- 请简述List接口的特点
- List是一个有序的集合,会把元素添加到指定的位置,允许重复元素添加到集合中。
- 有两种访问方式,随机访问,也就是用整数的索引来访问,另一个是用迭代器访问。
题目2
- 请简述HashSet去除重复元素的原理
如果要说清楚的话应该是挺复杂的,所以这里不说这么详细,只说一下大概的内容。我是这么理解的,可以分为两部分:- 这个使用散列函数来去重的,也就是当HashSet中要添加一个元素的话,会调用这个元素的hashcode方法来获取出哈希值,你也可以这么认为这个是一个地址,如果这个地址上没有元素存在,就可以存了
- 如果这个有元素存在,那么就调用这个元素的equals方法来比较,比较的内容就是这两个元素的具体内容,如果比较是真的,那么就认为这两个元素是相同元素,就不用存了,这就是去重了。
题目3
- 简述常见的数据结构中元素的存取特点(这里主要是关注它们的存取特点)
-
栈
是一个先进后出的存储特点。先存进去的元素,后取出来。 -
队列
是一个先进先出的存储特点。先存进去的元素,先取出来。 -
数组
这个是有序的元素序列,可以指定具体的索引来存放元素(不超过指定的容量),在内存上要求是连续的。
优点就是:
存进去的比较快,取出来的速度也比较快,
缺点就是:
如果要删除和添加到中间时,要移动比较多的元素,很慢,很麻烦,这是劣势 -
链表
这个就像是一条锁链那样,需要衔接,在内存上不要求是连续的。
优点:对于删除和增加元素是比较好的,不需要移动元素,只需要修改其中的存储地址即可。
缺点:要查找元素的话,要从头查找,如果元素过多,要查很多的时间
-
题目4
- 简述Comparable和Comparator两个接口的区别
- Comparable:强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方法。只能在类中实现compareTo()一次,不能经常修改类的代码实现自己想要的排序。实现此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序,对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。
- Comparator强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。
这道题我不太了解,所以这是网上的参考答案
题目5
- 根据要求练习LinkedList方法
(1)基本方法:add, set, get, remove, clear, size等方法;
(2)特有方法:addFirst, addLast, getFirst, getLast, removeFirst, removeLast, push, pop, clear等方法。
public class Test01 {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("hello");
System.out.println(linkedList);
//进行替换
linkedList.set(0,"world");
System.out.println(linkedList);
//取下标为0的元素
System.out.println(linkedList.get(0));
System.out.println(linkedList.remove(0));
linkedList.add("java");
System.out.println(linkedList);
linkedList.clear();
System.out.println(linkedList.size());
linkedList.addFirst("one");
linkedList.addLast("last");
System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());
System.out.println(linkedList.removeFirst());
System.out.println(linkedList.removeLast());
linkedList.push("push");
System.out.println(linkedList.pop());
}
}
题目6
- 定义人类,包含姓名和年龄属性。创建4个人存储到HashSet中,姓名和年龄相同的人看做同一人不存储。
代码如下:
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 class Test02 {
public static void main(String[] args) {
HashSet<Person> hashSet = new HashSet<Person>();
hashSet.add(new Person("zhangsan",11));
hashSet.add(new Person("lisi",14));
hashSet.add(new Person("wangwu",15));
hashSet.add(new Person("liliu",16));
for(Person person: hashSet){
System.out.println(person.getName()+"\t"+person.getAge());
}
}
}
题目7
- 向list集合添加姓名{张三,李四,王五,二丫,钱六,孙七},将二丫替换为王小丫
代码如下:
public class Test03 {
public static void main(String[] args) {
List list= new ArrayList();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("二丫");
list.add("钱六");
list.add("孙七");
//indexOf方法只能找到第一个二丫的位置
list.set(list.indexOf("二丫"),"王小丫");
//打印
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
题目8
- 使用LinkedHashSet存储以下元素:“王昭君”,“王昭君”,“西施”,“杨玉环”,“貂蝉”。使用迭代器和增强for循环遍历LinkedHashSet。
public class Test04 {
public static void main(String[] args) {
LinkedHashSet<String> linkedHashSet = new LinkedHashSet();
linkedHashSet.add("王昭君");
linkedHashSet.add("王昭君");
linkedHashSet.add("西施");
linkedHashSet.add("杨玉环");
linkedHashSet.add("貂蝉");
//迭代器遍历
Iterator iterator = linkedHashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//for增强遍历
for (String string:linkedHashSet
) {
System.out.println(string);
}
}
}
题目9
- 九、ArrayList集合中有如下内容: {33,11,77,55},使用Collections.sort()对ArrayList集合中的数据进行排序,并打印出排序后的结果。
public class Test05 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(33);
arrayList.add(11);
arrayList.add(77);
arrayList.add(55);
Collections.sort(arrayList);
System.out.println(arrayList);
}
}