A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© bayshier 中级黑马   /  2014-9-19 10:22  /  1287 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 bayshier 于 2014-9-21 17:45 编辑

20. 写一个类,在其中创建一个已经初始化的对象数组。使用此数组填充List。使用subList()生成此List的子集,然后使用removeAll()将子集从List中移除。
21. 修改第七章的练习6,使用ArrayList保存Rodent,并使用Iterator遍历Rodent的序列。记住,ArrayList只能保存Object,所以在使用其中Rodent元素时必须做类型转换。
    ( 6. 创建一个Rodent(啮齿动物):Mouse(老鼠),Gerbil(鼹鼠),Hamster(大颊鼠)这样的继承层次结构。在基类中,提供对所有的Rodent都通用的方法,在基类中,根据特定的Rodent类型重载这些方法,以便执行不同的行为。创建一个Robent数组,填充不同的Rodent类型,然后调用基类方法,观察发生什么情况。
22. 依据 Queue.java的例子,创建Deque(双头开口队列,头尾都可以插入的队列)类,并做测试。
//: c11:Queue.java
// Making a queue from a LinkedList.
import com.bruceeckel.simpletest.*;
import java.util.*;
public class Queue {
private LinkedList list = new LinkedList();
public void put(Object v) { list.addFirst(v); }
public Object get() { return list.removeLast(); }
public boolean isEmpty() { return list.isEmpty(); }
public static void main(String[] args) {
Queue queue = new Queue();
for(int i = 0; i < 10; i++)
queue.put(Integer.toString(i));
while(!queue.isEmpty())
System.out.println(queue.get());  
}
} ///:~
23. 在Statistics.java使用TreeMap。添加代码,测试HashMap和TreeMap的性能区别。
//: c11:Statistics.java
// Simple demonstration of HashMap.
import java.util.*;
class Counter {
int i = 1;
public String toString() { return Integer.toString(i); }
}
public class Statistics {
private static Random rand = new Random();
public static void main(String[] args) {
Map hm = new HashMap();
for(int i = 0; i < 10000; i++) {
// Produce a number between 0 and 20:
Integer r = new Integer(rand.nextInt(20));
if(hm.containsKey(r))
((Counter)hm.get(r)).i++;
else
hm.put(r, new Counter());
}
System.out.println(hm);
}
} ///:~
24. 生成一个Map和Set,使其包含所有以“A开头的国家。
27. 使用同样的数据多次填充Set,然后验证此Set中没有重复的元素。使用不同的Set做此测试。
28. 修改Statistics.java,写一个程序重复做测试,观察是否某个数字比别的数字出现的次数多。
29. 使用Counter对象的HashSet重写Statistics.java(修改Counter使其可以在HashSet中使用)。看看哪种方法更好?
30. 使用String“键”和你选择的对象填充LinkedHashMap。然后从中提取键值对,以键排序,然后重新插入此Map。
31.在ToyTest.java中,使用反射机制,通过非缺省构造器创建Toy对象。
//: c10:ToyTest.java
// Testing class Class.
interface HasBatteries {}
interface Waterproof {}
interface Shoots {}
class Toy {
// Comment out the following default constructor
// to see NoSuchMethodError from (*1*)
Toy() {}
Toy(int i) {}
}
class FancyToy extends Toy
implements HasBatteries, Waterproof, Shoots {
FancyToy() { super(1); }
}
public class ToyTest {
private static Test monitor = new Test();
static void printInfo(Class cc) {
System.out.println("Class name: " + cc.getName() +
" is interface? [" + cc.isInterface() + "]");
}
public static void main(String[] args) {
Class c = null;
try {
c = Class.forName("FancyToy");
} catch(ClassNotFoundException e) {
System.out.println("Can't find FancyToy");
System.exit(1);
}
printInfo(c);
Class[] faces = c.getInterfaces();
for(int i = 0; i < faces.length; i++)
printInfo(faces);
Class cy = c.getSuperclass();
Object o = null;
try {
// Requires default constructor:
o = cy.newInstance(); // (*1*)
} catch(InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch(IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
}
printInfo(o.getClass());
monitor.expect(new String[] {
"Class name: FancyToy is interface? [false]",
"Class name: HasBatteries is interface? [true]",
"Class name: Waterproof is interface? [true]",
"Class name: Shoots is interface? [true]",
"Class name: Toy is interface? [false]"
});
}
} ///:~
32.请在java.sun.com提供的Java HTML文档中找出java.lang.Class的接口。写一个程序,使它能够接受命令行参数所指定的类名称。然后使用Class的方法打印该类所有可以获得的信息。用标准程序库的类和你自己的写的类,分别测试这个程序。

33.从网上得到一个新闻的RSS xml文件,解析这个XML文件

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马