- 论坛徽章:
- 0
|
也许《Think in Java》里的有关容器的内容会对问题的解决有所帮助,下面的代码来自《Think in Java》,个人在Eclipse、ubuntu上运行通过。
------------遍历打印-------------------- /**
- * File:MapDataTest.java
- * ----------------------
- * This sample is a code piece in <Think in Java> "Containers in Depth"
- * It can run on Eclipse4.2,ubuntu12.04,OpenJdk7, you can download attachment,it is a Eclipse project.
- * And import "Existing Projects into Workspace", so you can test it.
- * Thinking in Java 4th Edition Source Code url:http://www.mindviewinc.com/TIJ4/CodeInstructions.html
- * OpenJDK™-7 Source code url:http://download.java.net/openjdk/jdk7/
- *
- */
- package containers;
- import java.util.*;
- import net.mindview.util.*;
- import static net.mindview.util.Print.*;
- class Letters implements Generator<Pair<Integer,String>>,
- Iterable<Integer> {
- private int size = 9;
- private int number = 1;
- private char letter = 'A';
- public Pair<Integer,String> next() {
- return new Pair<Integer,String>(
- number++, "" + letter++);
- }
- public Iterator<Integer> iterator() {
- return new Iterator<Integer>() {
- public Integer next() { return number++; }
- public boolean hasNext() { return number < size; }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- }
- public class MapDataTest {
- public static void main(String[] args) {
- // Pair Generator:
- print(MapData.map(new Letters(), 11));
- // Two separate generators:
- print(MapData.map(new CountingGenerator.Character(),
- new RandomGenerator.String(3), 8));
- // A key Generator and a single value:
- print(MapData.map(new CountingGenerator.Character(),
- "Value", 6));
- // An Iterable and a value Generator:
- print(MapData.map(new Letters(),
- new RandomGenerator.String(3)));
- // An Iterable and a single value:
- print(MapData.map(new Letters(), "Pop"));
- }
- } /* Output:
- {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K}
- {a=YNz, b=brn, c=yGc, d=FOW, e=ZnT, f=cQr, g=Gse, h=GZM}
- {a=Value, b=Value, c=Value, d=Value, e=Value, f=Value}
- {1=mJM, 2=RoE, 3=suE, 4=cUO, 5=neO, 6=EdL, 7=smw, 8=HLG}
- {1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop}
- *///:~
复制代码
MapDataTest.tar.gz
(14.64 KB, 下载次数: 8)
|
|