2

In Java (or any OOP language), I can make a nested class "Node", that represents some object that contains int data. I can then declare a "Node" reference with variable named "head":

public class ListNode {

    class Node {
        int value;
        Node next = null;

    public Node(int value) {
        this.value=value;
        }
    }

Node head;
}

I can then do something like:

head = new Node(42);

...which should make the "head" reference point to a new Node object with int value 42, and a reference inside of it pointing to null:

enter image description here

But how can I actually make another reference that points to the "head" reference? I don't think its possible, but it would look like this: enter image description here

I know that if I make another reference, like Node another_ref; and then do another_ref = head, all that will happen is another_ref will point to whatever head was pointing to (Node that contains 42 in this case), so I will be left with 2 references pointing at a Node object with int value = 42, and Node next reference that points to null.

I know that I can take head.next's reference, which is the Node object's reference pointing to null, and I can point it back at itself by doing head.next = head;, but it doesn't seem like I can get anything to point back to the Node head reference.

Am I missing something here? Can you not "chain" or "link" together references like this? Obviously I can create a bunch of new Nodes(integer values...) with various integer values to chain-together a linked list, but just curious about this.

ennth
  • 1,698
  • 5
  • 31
  • 63
  • 3
    Java does not expose pointers to the programmer. Thus, what you want is not possible in Java. What you can do, however, is let another reference reference the same object as `head`: `Node anotherReference = head;`. – Turing85 Jun 12 '20 at 07:30
  • As a C++ guy it's ironic to see how Java tries its best to abstract away pointer/reference issues, yet every day there are people in SO / [tag:java] asking questions like this one... – nada Jun 12 '20 at 08:15

3 Answers3

2

In C++, you can have pointers to pointers; you don't have that in Java. You can simulate it though:

Say you have the following Ref template interface:

public interface Ref<T> {
    T get();
    void set(T newValue);
}

Your Node class could have a method that returns a reference to it's next field:

    public Ref<Node> getRefToNext() {
        return new Ref<Node>(){
            @Override
            public Node get() {
                return next;
            }
            @Override
            public void set(Node newValue) {
                next = newValue;
            }
        };
    }

And class ListNode could have a method that returns a reference to it's head field:

public Ref<Node> getRefToHead() {
    return new Ref<Node>(){
        @Override
        public Node get() {
            return head;
        }

        @Override
        public void set(Node newValue) {
            head = newValue;
        }
    };
}

Now if you want to write a method that inserts a new node while keeping the list sorted, you can do:

public void insert(int value) {
    Ref<Node> ref = getRefToHead();
    Node node;
    while ((node = ref.get()) != null && value < node.value) {
        ref = node.getRefToNext();
    }
    Node newNode = new Node(value);
    newNode.next = node;
    ref.set(newNode);
}

But of course, it's much slower than with C++ pointers.

UPDATE

It allows you to reuse common code: Let's say you define a locate method as follows:

private Ref<Node> locate(int value) {
    Ref<Node> ref = getRefToHead();
    Node node;
    while ((node = ref.get()) != null && value < node.value) {
        ref = node.getRefToNext();
    }
    return ref;
}

You can use it to find a node:

public Node find(int value) {
    return locate(value).get();
}

And to insert a new node:

public void insert(int value) {
    Ref<Node> ref = locate(value);
    Node newNode = new Node(value);
    newNode.next = ref.get();
    ref.set(newNode);
}

The same principle can be used for binary trees.

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
1

It would be helpful to know what it is that you're trying to achieve. If you're trying to create a sequence of nodes pointing to each other, that's quite easy to achieve:

public class Node {
  private int value;
  private Node next;

  public Node(int value) {
    this(value, null);
  }

  public Node(int value, Node next) {
    this.value = value;
    this.next = next;
  }

  public int value() {
    return value;
  }

  public Node next() {
    return next;
  }
}

In that case, the class doesn't need to be nested, but can be part of its own file, by the way.

bertilmuth
  • 276
  • 1
  • 11
  • Not the same thing, he want create a reference of his object – Adrian Lagartera Jun 12 '20 at 07:45
  • Sure, I'm just trying to understand what he wants to use it for in practice. – bertilmuth Jun 12 '20 at 07:48
  • Yep, I wasn't trying to create a linked list, but thank you for asking. I was essentially trying to Reverse a LinkedList in place, and I wanted avoid some if/else conditionals by starting my 3 pointers at the HEAD, instead of the first node in the linkedlist (standard algorithm...). By then I realized, I could takes head's reference and point to what it points TO, but I couldn't conceive of a way to actually point AT HEAD..... – ennth Jun 12 '20 at 08:12
  • ...but yes, I know how to reverse a linkedl.ist, but this was more-so a hypothetical question in my mind ,, based out of curiosity (not really trying to achieve anything other than making 1 reference point to another) – ennth Jun 12 '20 at 08:16
  • If that's what you want to do: why not use Collections.reverse()? But I understand if it's just hypothetical. https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#reverse(java.util.List) – bertilmuth Jun 12 '20 at 09:54
1

Technically it cannot be done. As indicated by the user @Turing85

To do it as you want, you have to pass that object in a new instance. But technically it will not be a reference as such, it will be a new object.

In this post they explain it quite well with references. https://stackoverflow.com/a/2806564/11532975

I hope I've helped

Adrian Lagartera
  • 442
  • 1
  • 3
  • 17