7.
选择Vector、Stack、HashTable,因为这些集合类是线程安全的;如果多个线程只是只读,应该选用线程不安全的集合类,因为它们效率较高。
8.
import java.util.*;
class ComplexNumber{
int x, y;
ComplexNumber(int _x,int _y){
x=_x;
y=_y;
}
}
public class TestSort{
public static void main(String[] args){
List list= new ArrayList();
list.add(new ComplexNumber(9,6));
list.add(new ComplexNumber(7,6));
list.add(new ComplexNumber(5,6));
list.add(new ComplexNumber(8,6));
list.add(new ComplexNumber(10,6));
Collections.sort(list,new Comparator(){
public int compare(Object o1, Object o2){
ComplexNumber c1,c2;
c1 =(ComplexNumber)o1;
c2 = (ComplexNumber)o2;
return (c1.x-c2.x);
}
});
for(int i=0;i<5;i++){
ComplexNumber c = (ComplexNumber)list.get(i);
System.out.println("ComplexNumber"+i+":x="+c.x+",yi="+c.y);
}
}
}
9.
不一定。
10.
import java.util.*;
public class UnifiedException
{
private HashMap ht;
public UnifiedException(){
ht = new HashMap();
ht.put("/ by zero","发生了除数为零异常");
ht.put(null,"发生了空指针异常");
}
public void dealException(Exception e){
String str = e.getMessage();
System.out.println((String)ht.get(str));
}
public static void main(String[] args)
{
int a,b,c;
try{
a=67; b=0;
//c=a/b;
throw new NullPointerException();
}
catch(Exception e){
//System.out.println(e.getMessage());
new UnifiedException().dealException(e);
}
}
}