This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes acessing & adding to data structure elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "acessing & adding to data structure elements" Watch "acessing & adding to data structure elements" New topic
Author

acessing & adding to data structure elements

Graham Clark
Greenhorn

Joined: Jun 13, 2002
Posts: 17
Hi,
hopefully this is an easy question:
if you create a data structure in java (eg, a linked list), how would you access a particular node and, say, change the data in it, leaving the rest of the data structure unchanged? So if you've got a linked list of 3 elements, like
47 -> 38 -> 24
how would you go about changing the 2nd element to 37? Assuming the code for the structure is something like

If you were to use the getNext method, you'd get a copy of the second element rather than accessing the real second element, which isn't what I want.
Any help would be much appreciated.
thanks,
Graham.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Your Node class should only contain "node" stuff. A method like "getNext()" is a function on the list, not the node.
Strip down your Node class and make another class called "LinkedList" which makes use of the Node class. The LinkedList class would contain methods like "Add()" and "Next()".


Please ignore post, I have no idea what I am talking about.
 
Consider Paul's rocket mass heater.
 
subject: acessing & adding to data structure elements
 
Similar Threads
Guidance required on LinkedLists
a simple problom
a simple but complex problom
A challenge
RefList help