Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LinkedListIterator methods

 
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
For homework, we are to write five methods that works the same way as listiterator.

http://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html

The methods we have to write are: hasPrevious(), previous(), previousIndex(), remove(), and set(E e).

This is what I currently have:

CODE REMOVED.

I haven't yet written set(E e) or remove().

Right now I'm more confused by the previous-related methods.

If I have a list that contains (1, 2, 3) and I start the iterator at index 3, this is what my code outputs


The values in the [] were inputted by me as to what is expected. Is my thinking correct or are my expected values wrong?

Thanks in advanced.




 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Aj Prieto wrote:If I have a list that contains (1, 2, 3) and I start the iterator at index 3, this is what my code outputsThe values in the [] were inputted by me as to what is expected. Is my thinking correct or are my expected values wrong?


Well, there's more than one way to do it, and it would actually be better if you showed us ALL the code for the class, especially the fields you're using.
It's also a bit difficult to understand your output, but I would remind you that Java List and array indexes are 0-BASED.

However, the general idea is this:
If you're starting with an index that is outside the bounds (as 3 is for an array of size 3), then just treat it as 'exclusive' (ie, each call to hasPrevious()/previous() checks or returns the element before the 'current' index.
So, assuming you have a 'currentIndex' field, hasPrevious() might look something like:
return currentIndex > 0 && currentIndex <= list.size();

remove() is a bit trickier, but it's usually supposed to remove the current element, so you generally don't need to update any indexes. The same with set() I believe: it simply inserts a value at the current index.

The real tricky part comes in combining hasPrevious()/previous() and hasNext()/next(), but it doesn't appear that you need to worry about that yet.

HIH

Winston
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I've updated the first post to include the entire class. The methods marked with //TODO are the ones we have to do and the others were provided. A Double Linked Node list is used for the tests.

Thanks
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Aj Prieto wrote:A Double Linked Node list is used for the tests.


Ah. Well, in that case, implementation will be a bit different.

Winston
 
Marshal
Posts: 79177
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not remove code from, or alter, old posts. That makes the remainder of the thread incomprehensible.
If you think somebody has plagiarised your code, your college will find that because there is software which trawls the internet looking for similar code. I am sure it will find the code in your post.

I think little will be gained from further discussion, and am closing this discussion.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic