Skip to content

Commit c458f83

Browse files
committed
工厂方法模式
1 parent 9fae469 commit c458f83

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/com/anxpp/designpattern/factorymethod/LinkList.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ public Iterator<T> iterator() {
1111
public boolean add(T t) {
1212
if(size==0){
1313
first = new Node<T>(t,null);
14+
size++;
15+
return true;
1416
}
15-
while(first.next!=null);
16-
first.next = new Node<T>(t,null);
17+
Node<T> node = first;
18+
while(node.next!=null)
19+
node = node.next;
20+
node.next = new Node<T>(t,null);
21+
size++;
1722
return true;
1823
}
1924
//链表节点
@@ -33,7 +38,7 @@ private class MyIterator implements Iterator<T>{
3338
}
3439
@Override
3540
public boolean hasNext() {
36-
return next!=null;
41+
return next != null;
3742
}
3843
@Override
3944
public T next() {

src/com/anxpp/designpattern/factorymethod/TestUse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String args[]){
1111
}
1212
//获得迭代器
1313
Iterator<Integer> ai = array.iterator();
14-
Iterator<Integer> li = array.iterator();
14+
Iterator<Integer> li = link.iterator();
1515
//遍历并输出
1616
while(ai.hasNext())
1717
System.out.print(ai.next());

0 commit comments

Comments
 (0)