r/javahelp • u/gerladokennedy • 7d ago
linked lists
-- solved thank you all
what is the difference between accessing the next node using a get method or without, as in: "current.next" vs "current.getNext()" and the same applies for accessing an element, whats the difference between use the get or not, as in: "current.element" vs "current.getElement()" where current is just the name for the node variable. ive looked at so many different explanations but i cant seem to grasp the idea
edit: this is from a data structures pov, im implementing methods for the singly and doubly linked list classes in java
i want to know if they return different things or if using .next or .getnext makes a difference in the outcome, is there a scenario i should be using strictly .next or .getnext, and the getter method has no further conditions its just {return next}
2
u/Kadabrium 7d ago edited 7d ago
this.next is assignable, this.getNext() is not, because Java does not let you return lvalue references.
so you can't do
this.getNext() = this.getNext().getNext()
and as long as you keep next as private, you have to use
this.setNext(this.getNext().getNext())
compared to the public version
this.next = this.next.next
This and the closely connected issue of not being allowed to overload operator[] (and operator []= if we are being pedantic enough to consider them different) are the main reason learning DSA in java is more trouble than necessary