JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Software Design » Design Pattern  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 問關於Iterator Pattern的問題
vxr





發文: 11
積分: 0
於 2009-04-23 12:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
是這樣的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class StructureB extends Structure{
    private int MAX_SIZE=10;
    private int numberOfItems=0;
    private Item[] items=new Item[MAX_SIZE];
    public StructureB(String name){
        this.name=name;
        addItem(new Item("Mouse"));
        addItem(new Item("Keyboard"));
    }
    public String getName(){
        return name;
    }
    protected void addItem(Item item){
        if(numberOfItems<MAX_SIZE){
            items[numberOfItems]=item;
            ++numberOfItems;
        }else throw new java.lang.ArrayIndexOutOfBoundsException();
    }
 
    public Iterator createIterator(){
        return new IteratorOfItemArray(items);
    }
}
//----------------------------------------------------------------------------------------------------
 
public class IteratorOfItemArray implements Iterator{
   private Item[] items;
   private int position=0;
   public IteratorOfItemArray(Item[] item){
        this.items=items;
   }
 
    public Object next(){
       Item item=items[position];
       position++;
        return item;
    }
 
    public boolean hasNext(){
        if(position>=items.length||items[position]==null)
            return false;
        else return true;
    }
    public void remove(){
        throw new UnsupportedOperationException();
    }
}
//----------------------------------------------------------------------------------------------------
 
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
 
        Structure structureB=new StructureB("This is a B!");
 
        java.util.Iterator iteratorB=structureB.createIterator();
        printItem(iteratorB);
 
    }
    public static void printItem(java.util.Iterator iterator){
        while(iterator.hasNext()){
            System.out.println(((Item)iterator.next()).getName());
        }
    }
}

結果運行起來一直給我報exception...
Exception in thread "main" java.lang.NullPointerException
at iterator.IteratorOfItemArray.hasNext(IteratorOfItemArray.java:26)
at iterator.Main.printItem(Main.java:28)
at iterator.Main.main(Main.java:24)
Java Result: 1

看了老半天...
我實在不了解錯在哪裡...!!
希望有高手幫我解惑...
感謝...!!


vxr edited on 2009-04-23 13:03
reply to postreply to post
作者 Re:問關於Iterator Pattern的問題 [Re:vxr]
maisky





發文: 85
積分: 0
於 2009-04-23 21:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
public class IteratorOfItemArray implements Iterator{
   private Item[] items;
   private int position=0;
   public IteratorOfItemArray(Item[] item){
        this.items=items;     //  <<====   
   }
 
}
 


標示的這行錯了, input argument 是 item, 不是 items.

1
this.items = item;


reply to postreply to post
» JWorld@TW »  Software Design » Design Pattern

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8