// Creating a doubly linked list.
class DoublyLinked {
doubleLinkedList = new DoubleLinkedList();
// This insert into Doubly Linked List
doubleLinkedList.insert("john")
doubleLinkedList.insert("ssss")
doubleLinkedList.insert("ttt")
//After insertion, I want ti implement Iteratot Design Pattern to Display the linked list.
//So how can we achieve this- Any kind of suggestions will be appreciated.
}
class DoubleLinkedList {
private NewLink firstNode;
private NewLink lastNode;
private NewLink rootNode;
public DoubleLinkedList() {
firstNode = null;
lastNode = null;
}
}
class NewLink {
public String data;
public NewLink nextPointer;
public NewLink previousPointer;
public NewLink(String id) {
data = id;
}
public String toString() {
return "{" + data + "} ";
}
}
7 Replies - 1309 Views - Last Post: 19 February 2012 - 05:52 PM
#1
Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:04 PM
I am new to design patterns. And I was trying to implement Iterator Design Patter in Java in Linked list to display the linked list from front.
Replies To: Implementation of Iterator Design Pattern in Java
#2
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:19 PM
At the very least, you should create an insert method first, so that you can test your iterator.
#3
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:23 PM
Insert method is already created and its working fine. I haven't posted it. I just need to work on iterator design pattern so that I can iterate.
#4
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:31 PM
1. Have your list implement Iterable.
2. Have the iterator method return an implementation of Iterator for your list.
That's basically it.
2. Have the iterator method return an implementation of Iterator for your list.
That's basically it.
import java.util.Iterator;
class MyList<E> implements Iterable<E> {
public Iterator<E> iterator() {
return new MyListIterator();
}
private class MyListIterator implements Iterator<E> {
@Override
public boolean hasNext() {
return false;
}
@Override
public E next() {
return null;
}
@Override
public void remove() {
}
}
}
This post has been edited by blackcompe: 19 February 2012 - 05:51 PM
Reason for edit:: Updated code.
#5
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:37 PM
Personally I wouldnt have my Iterator declare its own type if it is not implementing its own methods
to
private class MyListIterator<V> implements Iterator<V>
to
private class MyListIterator implements Iterator<V>
This post has been edited by ianian112: 19 February 2012 - 05:39 PM
#6
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:41 PM
Quote
Personally your Iterator should not have a type declared with it unless you plan to implement your own methods
Can you clarify? I don't quite understanding what you're saying. Removing the formal type parameter breaks my code.
This post has been edited by blackcompe: 19 February 2012 - 05:41 PM
#7
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:44 PM
blackcompe, on 19 February 2012 - 05:41 PM, said:
Quote
Personally your Iterator should not have a type declared with it unless you plan to implement your own methods
Can you clarify? I don't quite understanding what you're saying.
Since all the methods in your iterator class come from the interface Iterator, they will all use the generic type that the iterator has
public class Iter<E> implments Iterator<Z>
the next method will return an object of type Z. This means that if you are only overriding the methods in Iterator, there is no need for a generic type on Iter
This post has been edited by ianian112: 19 February 2012 - 05:46 PM
#8
Re: Implementation of Iterator Design Pattern in Java
Posted 19 February 2012 - 05:52 PM
Quote
the next method will return an object of type Z. This means that if you are only overriding the methods in Iterator, there is no need for a generic type on Iter
I see. Glad you caught that. Thanks.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|