Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Iterator problems

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterator problems

    Hi everyone.
    I wrote this interface : Dictionary<K, V> extends Iterable<K>
    and this class : LinkedList<K, V> implements Dictionary<K,V> but i not have idea how to write iterator for this class. Someone can help me ?
    thank you!



    public interface Dictionary<K, V> extends Iterable<K> {
     
    	public void insert(K key, V value);
    	public void delete(K key);
    	public V search(K key);
    }



    public class LinkedList<K, V> implements Dictionary<K,V>{
     
    	Record list = null;
     
    	class Record<K,V> {
    		K key;
    		V value;
    		Record next;
    		Record prev;
     
    		Record(K key, V value){
    			this.key = key;
    			this.value = value;
    		}
    	}
     
     
    	@Override
    	public Iterator<K> iterator() {
    		// TODO Auto-generated method stub
    		return null;
    	}
     
    	@Override
    	public void insert(K key, V value) {
    		Record nuovo = new Record(key, value);
    		if(list==null){
    			list = nuovo.next = nuovo.prev = nuovo;
    		}else{
     
    			nuovo.next = list.next;
    			list.next.prev = nuovo;
    			list.next = nuovo;
    			nuovo.prev = list;
    		}
     
    	}
     
    	@Override
    	public void delete(K key) {
    		Record p = null;
    		if(list!=null)
     
    			for(p=list.next;;p=p.next){
    				if(p.key.equals(key))
    					break;
    				if(p==list){
    					p = null;
    					break;
    				}
     
    			}
     
     
    		if(p==null)
    			throw new RuntimeException("element not found");
    		if(p.next == p)
    			list = null;
    		else {
    			if(list==p)
    				list = p.next;
    				p.next.prev= p.prev;
    				p.prev.next = p.next;
    		}
     
    	}
     
    	@Override
    	public V search(K key) {
    		if(list==null)
    			throw new RuntimeException("lista vuota");
    		for(Record p =list.next;;p=p.next){
    			if(p.key.equals(key))
    				return (V) p.value;
    			if(p==list)
    				return null;
    		}
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Iterator problems

    What should the methods of the Iterator object return?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Iterator problems

    Quote Originally Posted by Norm View Post
    What should the methods of the Iterator object return?
    Should be Record!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Iterator problems

    public interface Dictionary<K, V> extends Iterable<K> {
    The Iterator is defined for type K not Record.

    The code has:
    	public Iterator<K> iterator() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 08:51 PM
  2. stuck on Iterator
    By Mjall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 11:00 PM
  3. iterator help needed
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 12:32 PM
  4. Iterator Problem
    By chrisych in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 10th, 2010, 12:29 PM
  5. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM