• Post Reply Bookmark Topic Watch Topic
  • New Topic

unmapped collection.  RSS feed

 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually have two questions, this is the first.

I have coded this collection to have three objects but the loop will not display them. Any help is appreciated. Thanks a lot.



This is the second.

I'm trying to code the collections string objects in reverse order without using a list iterator. Again, any suggestions or guidance is greatly appreciated. Thanks a lot.

 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your first exercise and it won't compile. It says "method size is undefined for type Iterator." Yes, if you look up the Iterator interface in the API specifications you find it has exactly three methods, and size isn't one of them. You shouldn't find it too difficult to workout which method you actually want (you have only got three to choose from ).

You have an Iterator which is created from a Set which has no objects contained in it. So it prints them out, all none of them. Maybe if you created the Iterator after populating the Set you would get different results.

Now I shall have a look at the second example.
 
Karan Rajan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have coded this collection to have three objects but the loop will not display them.




First thing, you are getting the iterator from the empty set and then adding elements to your set. Thus you are iterating over an empty iterator. Another thing is that there is no "size()" method on the iterator. You should be seeing a compiler error right there.

To see the elements in the set, create your iterator after adding the elements, and in the while-loop fetch the contents of the iterator using the "next()". Here's a link to the iterator api page.



I'm trying to code the collections string objects in reverse order without using a list iterator.

OK, i'm a little unsure about what you are asking here ... but ... if you just want retrieve the string-objects in the reverse order of insertion ...
then have you thought about using a stack ?

Hope that helps !
[ February 12, 2007: Message edited by: Karan Rajan ]
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your second exercise prints out the Iterator, rather than the objects in the Iterator.
Each of your calls creates a new ListIterator starting at member no 3. You will have to go into the API specifications (here for JDK6), and find Iterator and ListIterator and see which methods they have. Then you can work out what to call. And remember Iterators don't have an elementAt(int) method.
[ February 12, 2007: Message edited by: Campbell Ritchie ]
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
I tried your first exercise and it won't compile. It says "method size is undefined for type Iterator." Yes, if you look up the Iterator interface in the API specifications you find it has exactly three methods, and size isn't one of them. You shouldn't find it too difficult to workout which method you actually want (you have only got three to choose from ).

You have an Iterator which is created from a Set which has no objects contained in it. So it prints them out, all none of them. Maybe if you created the Iterator after populating the Set you would get different results.

Now I shall have a look at the second example.


Okay, I have modified my code and it works. I'm wondering if there is a way I can display the list in the opposite order it is coded in.
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ken zemaitis:


Okay, I have modified my code and it works. I'm wondering if there is a way I can display the list in the opposite order it is coded in.


Nevermind, first code has been corrected.
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Your second exercise prints out the Iterator, rather than the objects in the Iterator.
Each of your calls creates a new ListIterator starting at member no 3. You will have to go into the API specifications (here for JDK6), and find Iterator and ListIterator and see which methods they have. Then you can work out what to call. And remember Iterators don't have an elementAt(int) method.

[ February 12, 2007: Message edited by: Campbell Ritchie ]


I can now get the objects to display, but I am having trouble with getting them to display in the opposite order they are in. Any more suggestions?
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karan Rajan:

OK, i'm a little unsure about what you are asking here ... but ... if you just want retrieve the string-objects in the reverse order of insertion ...
then have you thought about using a stack ?

Hope that helps !

[ February 12, 2007: Message edited by: Karan Rajan ]


I just got the first one working, thank you very much for your help. For the second problem, I am supposed to see if I can make this program loop to display the collection's String objects in reverse order WITHOUT using a ListIterator. The LinkedList class has a method for retrieving an object based upon its index and the last object always has an index equal to the size of the collection minus 1, but I do now know which one it is. I am just struggling. Any help is greatly appreciated.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were nearly there with the ListIterator, which will retrieve the objects in reverse order.
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You were nearly there with the ListIterator, which will retrieve the objects in reverse order.


Haha I can't use a listiterator, which may be why I am having so much trouble.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe a ListIterator only works on a List. I haven't checked. In which case you can't print the members of a Set in reverse order. You can't print them in normal order either, because when you enter members into a Set you lose the ordering information.
If you want to retain order you will have to use a class which implements the List interface.
 
Karan Rajan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The LinkedList class has a method for retrieving an object based upon its index and the last object always has an index equal to the size of the collection minus 1, but I do now know which one it is. I am just struggling. Any help is greatly appreciated.


The LinkedList also has a method for removing the last item in the list. Take a look at the documentation, now can you retrieve the objects in reverse order from a list ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!