File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes LinkedListIterator methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

LinkedListIterator methods

Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

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.





Da mihi sis bubulae frustum assae, solana tuberosa in modo Gallico fricta ac quassum lactatum coagulatum crassum.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Aj Prieto
Ranch Hand

Joined: Sep 28, 2012
Posts: 75

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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedListIterator methods