Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question about references

 
Ranch Hand
Posts: 35
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the other day to refresh my reference skills in java, so I created a single linked list with a head (not a tail). While I wrote the code intuitively, I think I haven't completely understand it. Could you please clarify it in detail?

This is the code where I remove an element from the list. I can't understand the head = head.next part (head is a pseudo-node which refers to the first node). I mean, if head.next refers to the node next to the first node, why head is the reference and not the first node? Tell me if you need further clarification. Thank you.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, you wrote that code, but you don't know what it does or why you wrote it that way?

Why don't you try drawing pictures of head and, say, a 3-element list, showing the data and pointers. Show what head points to and what each "next" points to. Then show what it looks like when you remove the first element. Then see if you can relate that to the code that you wrote.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, variables (of non-primitive types) are always references.

Maybe you know C++. If you're used to C++, this aspect of Java can be confusing, because in C++ a variable is the object itself, not a reference.

A reference in Java is a lot like a pointer in C++ - except that you can't do pointer arithmetic.

In your code, head refers to a node object in your list. I don't know what type of object that is, since you didn't show that here. Maybe you have a class called Node that has a member variable which is a reference to the next Node. Something like this:

In your own list class, you have a member variable named head that refers to the first Node object in the list:

So, what does this line do:

It looks at the first Node in the list, takes the next reference, and makes head point to that.
 
michael delta
Ranch Hand
Posts: 35
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Jesper de Jong

Bingo! Thanks!
 
The City calls upon her steadfast protectors. Now for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic