aspose file tools*
The moose likes Beginning Java and the fly likes Linked Lists Add Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linked Lists Add" Watch "Linked Lists Add" New topic
Author

Linked Lists Add

Cain Silverbane
Greenhorn

Joined: Jul 22, 2002
Posts: 13
Hi again, all. For class we are now writing our own add method for the linked lists under a class MyLinkedListIterator. Can anyone help point out what it is I am doing wrong? If you need to see more of my code, let me know. Thx.


~ Silverbane ~
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What problem are you having? Why are you throwing a NoSuchElementException from an add(Object) method? What does the addBefore(Object) method do? Why are you keeping a modification count?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
I'd make a couple of changes.
The first is that the exception type doesn't seem to convey the nature of the exception. An IllegalStateException or similar would perhaps be more fitting.
The other is that an addBefore method would usually require two arguements. One representing the object to add and the other representing the object to be added before. i.e. as it stands ... add before what ?
Cain Silverbane
Greenhorn

Joined: Jul 22, 2002
Posts: 13
Here is the add before...

As it stands now, I am having a lot of trouble with references... the program we are using runs a number of test cases, and if you get an error it tells you what you should have. It says I should be getting a number of ints, but instead I am getting memory locations.
Our teacher said to do the mod count... to make sure any other methods don't alter it without you knowing or some such...
Ah, and here is the actual link part...

As I see it, the addBefore means that it is added before m_current.
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
With out all of your code its gonna be a guessing game as to what your problem is.
Send me your code sih@ufl.edu. I'll be happy to take a look at it, when I get a chance. If you could zip all the files together that would be great.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
Hi,
It seems the problem is in the addBefore(Object o) method of your link class. It seems like you are missing a few lines. Here's what I've come up. I didn't know how to run your tests so i'm not sure if it works. Let me know.

When you are writing this code you have to keep in mind that this is a Doubly linked list. So you have to keep track of m_prev and m_next for all Link objects. Hope this helps.
Cain Silverbane
Greenhorn

Joined: Jul 22, 2002
Posts: 13
Hmm... that indeed help me on my way! Thanks a lot, to everyone. I really love how the 'community' here helps one another.
It seems I wasn't giving enough information to you all, and for that I apologize. The bulk of the problem revolved around the my mis-coding of the previous().... I had put a reference to m_lastReturned when it should have been m_current.m_prev. Then, after fixing the other parts (which was in part thx to people here , I got it running!
Thanks again, everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linked Lists Add
 
Similar Threads
Creating Linked List
Geek gang paraphernalia
How to interpret this?
translate to pig latin!? completely lost....
if-else condition thru Sequence diagram ?