1.Collection, list, Set之间的联系和区别?
Collection:是对象集合,Collection有两个子接口List和Set
List:可以通过下标(1.2…)来取值,值可以重复,存储顺序与插入元素顺序一致
Set:只能通过游标来取值,并且值是不能重复的(和数学中的集合一样),不保证元素存储顺序
2.遍历一个集合对象都有哪些方法?
有三种方法:for循环,Iterator迭代器接口以及for each循环
以list为例:
//for循环
for( int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i));
}
//Iterato迭代器
Iterator<String> iter = list.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
//for each
for(String str : list) {
//for(集合元素的类型 局部变量 : 集合对象)
//内部仍然调用了迭代器。
System.out.println(str);
}
3.同数组相比,vector有何特点?
定义:实现自动增长的对象数组
特点:
1、使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加
2、 Vector类提供额外的方法来增加、删除元素,比数组操作高效
4.Vector与ArrayList,LinkedList与ArrayList,Hashtable与HashMap,TreeMap与HashMap之间的共同点和区别?
Vector与ArrayList:
Hashtable与HashMap:
TreeMap与HashMap:
5.Map、Collection、Iterator之间的关系如何?
Map接口实现者调用接口方法values()
返回一个实现Collection接口的集合对象的引用
Collection接口的实现者有可调用iterator()
方法返回一个实现Iterator接口的对象引用
6.Collection和Collections各自的功能是什么?
Collection是一个接口,定义了一系列对集合操作的标准
Collections是针对Collection而开发的一个辅助类,定义了一系列对集合进行操作的静态方法
7.现需要选择集合类,它存储的对象集合可以被多个线程维护(增加、删除),请问应该选择什么样的集合类,为什么?如果多个线程只是读取,而不维护,应该选择什么样的集合类,为什么?
1.选择 Vector类,因为 Vector类的方法是同步的,是线程安全的
2.选择 ArrayList类,因为 ArrayList的方法不是线程同步的,其性能更高
8.如何实现集合对象排序?定义一个复数类并按照复数的实部大小对复数对象进行排序
//方法一:实现自身比较器Comparable
import java.util.*;
public class Complex implements Comparable<Complex>{
private int real, imagin;
public Complex(){
real =0;
imagin =0;
}
public Complex(int r , int i){
real = r;
imagin = i;
}
@Override
public int compareTo(Complex o) {
// TODO Auto-generated method stub
return this.real-o.real;
}
@Override
public String toString(){
if(imagin>0)
return real+"+"+imagin+"i";
else if(imagin==0)
return real+"";
else return real+""+imagin+"i";
}
public static void main(String[] args){
List<Complex> al = new ArrayList<Complex>();
al.add(new Complex(1,3));
al.add(new Complex(-1,0));
al.add(new Complex(10,-5));
System.out.println(al);//排序前
//用Collections类的静态方法排序
Collections.sort(al);
System.out.println(al);//排序后
}
}
//方法二:实现自定义比较器Comparator
import java.util.*;
public class Complex {
private int real, imagin;
public Complex(){
real =0;
imagin =0;
}
public Complex(int r , int i){
real = r;
imagin = i;
}
@Override
public String toString(){
if(imagin>0)
return real+"+"+imagin+"i";
else if(imagin==0)
return real+"";
else return real+""+imagin+"i";
}
public static void main(String[] args){
List<Complex> al = new ArrayList<Complex>();
al.add(new Complex(1,3));
al.add(new Complex(-1,0));
al.add(new Complex(10,-5));
System.out.println(al);//排序前
//用Collections类的静态方法排序
Collections.sort(al, new Comparator<Complex>(){
public int compare(Complex o1,Complex o2){
return o1.real < o2.real ? -1 : 1;
}
});
System.out.println(al);//排序后
}
}
9.编写程序测试Java集合框架中各种常用类的 基本操作(包括添加元素,删除元素,遍历集合元素等)
1.Vector:
从AbstractList派生而来,可自动增加容量来容纳所需对象,实现List接口,元素之间有序。
import java.util.*;
public class vector {
public static void main(String[] args) {
Vector<Integer> v=new Vector();
for(int i=1;i<=10;i++)//添加元素
v.addElement(new Integer(i));
for(Iterator it=v.iterator();it.hasNext();) {//遍历元素
Integer i=(Integer) it.next();
System.out.print(i+" ");
}
System.out.println();
v.remove(3);//删除元素
for(Iterator it=v.iterator();it.hasNext();) {
Integer i=(Integer) it.next();
System.out.print(i+" ");
}
System.out.println();
}
}
//输出结果
1 2 3 4 5 6 7 8 9 10
1 2 3 5 6 7 8 9 10
2.Stack:
从Vector派生出,增加了栈的实现方法
import java.util.*;
public class stack {
public static void main(String[] args) {
Stack<Integer> s=new Stack();
for(int i=0;i<10;i++)//入栈
s.push(new Integer(i));
s.add(new Integer(10));//添加
for(Iterator it=s.iterator();it.hasNext();) {//遍历
System.out.print((Integer)it.next()+" ");
}
System.out.println();
s.remove(3);//删除
for(Iterator it=s.iterator();it.hasNext();) {
System.out.print((Integer)it.next()+" ");
}
System.out.println();
s.pop();//出栈
for(Iterator it=s.iterator();it.hasNext();) {
System.out.print((Integer)it.next()+" ");
}
System.out.println();
while(!s.empty()) {
System.out.print(s.pop()+" ");
}
}
}
//输出结果
0 1 2 3 4 5 6 7 8 9 10
0 1 2 4 5 6 7 8 9 10
0 1 2 4 5 6 7 8 9
9 8 7 6 5 4 2 1 0
3.LinkedList:
从AbstractList派生而来,实现了一个链表,由这个类定义的链表也可以像栈和队列一样被使用
package chapter10;
import java.util.*;
public class linkedlist {
public static void main(String[] args) {
LinkedList<Integer> l=new LinkedList();
for(int i=0;i<5;i++)//从头添加
l.addFirst(new Integer(i));
for(int i=5;i<10;i++)//从尾添加
l.addLast(new Integer(i));
for(Iterator it=l.iterator();it.hasNext();) {//遍历
System.out.print((Integer)it.next());
}
System.out.println();
l.removeFirst();//从头删
l.removeLast();//从尾删
for(Iterator it=l.iterator();it.hasNext();) {//遍历
System.out.print((Integer)it.next());
}
System.out.println();
}
}
//输出结果
4321056789
32105678
4.ArrayList:
由AbstractList派生而来,规模可变,且能像链表一样被访问
import java.util.*;
public class arraylist {
public static void main(String[] args) {
ArrayList<Integer> al=new ArrayList();
for(int i=1;i<=10;i++)//增加元素
al.add(new Integer(i));
for(Iterator it=al.iterator();it.hasNext();)//遍历元素
System.out.print((Integer)it.next()+" ");
System.out.println();
al.remove(4);//删除元素
for(Iterator it=al.iterator();it.hasNext();)
System.out.print((Integer)it.next()+" ");
}
}
//输出结果
1 2 3 4 5 6 7 8 9 10
1 2 3 4 6 7 8 9 10