• Post Reply 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

acessing & adding to data structure elements

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic