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 7 of 7

Thread: creating an iterator without creating a class for it

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default creating an iterator without creating a class for it

    I was asked to add an iterator to a class called MyLinkedList<E> which extends MyAbstractList<E> (abstract class) which implements MyList<E> (interface) .

    Now I am asked to create the iterator without using the word "class"....I know how to create an iterator using a private class but wihtout using the word class at all? I am out of ideas....

    MyLinkedList has an inner class which is:
    private static class Node<E>

    I tried implementing the Iterator interface for MyLinkedList...

    But then how do I return the Itarator ? how do I instanciate it?
    REturning new iterator does not help since then it doesn;t have the size of the list.
    Usually when I have its own class I have no problem returning new itereator since it keeps in its class the size of the outter class' list, but when I do it like this , it creates a new list so size is 0....I thought of adding the implementation to the node but then it makes no sense since the node should;t know it is a part of a list....

    Any ideas?
    Inner class is not forbidden...only using the class word is...if it makes any sense


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: creating an iterator without creating a class for it

    Quote Originally Posted by Lenjaku View Post
    Now I am asked to create the iterator without using the word "class"....I know how to create an iterator using a private class but wihtout using the word class at all? I am out of ideas....
    Without the "class" keyword, the only way is .... an anonymous inner class!

    Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Quote Originally Posted by Lenjaku View Post
    But then how do I return the Itarator ?
    You shoud have a method named e.g. iterator(), getIterator() (or any name you want or you have been asked to use) with a return type of Iterator<E>

    Quote Originally Posted by Lenjaku View Post
    how do I instanciate it?
    See how to use anonymous inner classes.

    Quote Originally Posted by Lenjaku View Post
    REturning new iterator does not help since then it doesn;t have the size of the list.
    An anonymous inner class, created in the context of an instance method, has access to all instance variables of object in the container class.
    You can access that state or keep some state in the anonymous inner class instance. It's up to you for the "design" you choose.
    The "current" point into the list surely must be kept in the anonymous inner class instance.

    Again: learn anonymous inner classes.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

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

    Default Re: creating an iterator without creating a class for it

    First thank you very much for the help.

    I tried doing it but my Iterator wouldn;t iterate...it prints the first 1 only...
    It should (by what we were asked) start from the end to teh start so I set it;s start point to the size of the list and each time (since index starts from 0) I firstly decrease it then returns the right element by the index....well this is the idea.


    In order to see what happens I tried creating another method to return the current index but for some reason my main doesn;t recognize this method and I dun get why...this is what I have:

     Iterator<E> it=new Iterator<E>()
    	{
    		int index=size;	
     
    		@Override
    		public E next()
    		{		
     
    			index--;
    			return (E)get(index);							
     
    		}
    		@Override
    		public void remove() {
    			// TODO Auto-generated method stub
     
    		}
    		@Override
    		public boolean hasNext() {
    			// TODO Auto-generated method stub
    			return false;
    		}
    		public int getIndex()
    		{return index;}
     
    	};
     
     
    	public Iterator<E> iterator()
    	{
    		return  it;
    	}



    This is in my MyLinkedList class.

    On main I have
    Iterator it=lst.iterator();
     
    		it.getIndex();
    second line is treated as error....anything I did wrong?

    --- Update ---

    I added printing inside the next() method and for some reason even though the list's size is not 0 when I write in the iterator class I created
    int index=size

    it saves index as 0.
    I am confused.
    It recognizes the name of the variable but does not get the value....

    size is declared on the MyList interface while I wrote my Iterator for MyLinkedList

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: creating an iterator without creating a class for it

    Please read this topic to learn how to post code correctly and then apply it.

    Thanks.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating an iterator without creating a class for it

    forgot to add this:
    @Override
    		public boolean hasNext() {
    		if(index>0)
    			return true;
    			return false;
    		}
    Does not matter much though.

    The only problem is the initial value it gets for index ...
    It works when I enter a number but not when I set size.
    I guess it happens when it precompiles and not when I ask to get an Iterator so I should have a method to reset the Iterator but it doesn;t seem like I can add methods....and I can;t get to the inner classes parameter from teh outter class so it must be inside the inner class so I am confused.

    --- Update ---

    Ok I changed it to
    public  Iterator<E> iterator()
    	{
    		return new Iterator<E>(){
    			int index=size;
     
    			@Override
    			public E next()
    			{					
    				index--;
    				return (E)get(index);							
     
    			}
    			@Override
    			public void remove() {
    				// TODO Auto-generated method stub
     
    			}
    			@Override
    			public boolean hasNext() {
    				if(index>0)
    					return true;
    				return false;
    			}
    		};	
    	}


    And now it seems to work....I don;t get it still though >.<
    What was wrong with the previous version and what bugs me more is it did not have a return statement but it didn;t seem to care.....

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: creating an iterator without creating a class for it

    Quote Originally Posted by Lenjaku View Post
    What was wrong with the previous version
    You didn't post the entire class, but I can guess that in the previous version the

    Iterator<E> it = .....

    was an instance variable. This means at least 2 bad things:

    First, the iterator instance is created just when the list is instantiated, and the 'size' is presumably 0 at that moment. So the iterator doesn't "see" any element, even if you add elements.

    Second, being a single instance variable you have only that single iterator. But conceptually it'is perfectly possible and legal to have, for one list object, many iterators that can iterate independently.

    For these reasons you must instantiate a new iterator on every iterator() invocation.


    Three final notes:

    1) [important] Your list is a "linked" list ..... but get(index) has to scan the linked list to find the item at that index! Remember that the iterator can (and should) have an "intimate" vision on the internal structure of the linked list. Thus the iterator should carry the iteration in the smart way .... not in the trivial way call-the-get-on-each-next.

    2) If you have used the "generics" in an appropriate and consistent way, the cast (E)get(index) is not necessary.

    3) If you don't want to implement remove() with the appropriate behaviour, you can leave it empty but generally it's more common and useful to throw a java.lang.UnsupportedOperationException just to signal that it's not supported.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating an iterator without creating a class for it

    Quote Originally Posted by andbin View Post


    1) [important] Your list is a "linked" list ..... but get(index) has to scan the linked list to find the item at that index! Remember that the iterator can (and should) have an "intimate" vision on the internal structure of the linked list. Thus the iterator should carry the iteration in the smart way .... not in the trivial way call-the-get-on-each-next.
    I agree. Unfortunately we were asked to use existing methods.
    I would write a method that scans till current index -1 or something, pretty much does the same but without using external method.
    On the other hand we are asked to write it wisely so I guess I will still do it.


    Quote Originally Posted by andbin View Post
    2) If you have used the "generics" in an appropriate and consistent way, the cast (E)get(index) is not necessary.
    Seems Java asked to add casting, but I will look into it again, may be a leftover from my previous pathetic attempts.

    Quote Originally Posted by andbin View Post
    3) If you don't want to implement remove() with the appropriate behaviour, you can leave it empty but generally it's more common and useful to throw a java.lang.UnsupportedOperationException just to signal that it's not supported.
    3. Thank you, I will add it.


    Thank you very much for the help

Similar Threads

  1. [SOLVED] Help with creating a class and driver
    By JackCannon15 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 27th, 2011, 03:50 PM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. [SOLVED] Creating A Class
    By cb5950 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 31st, 2011, 07:27 AM
  4. [SOLVED] Class Creating Help
    By pitchblack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 11:25 PM
  5. Help with creating a class
    By cdawg_2010 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 1st, 2010, 07:04 AM