Skip to content
Advertisement

How do I create a reference to my object using a variable?

So I have an array full of nodes called “SNodes” (they are a class of my own creation. They are literally just a basic node that holds a single String and a pointer to the next node).

I have a method called insertValue() that takes in the index you want to put a value in and the String you want the SNode to contain. However, if the index passed already contains an SNode in it, I want the new value to become that SNode‘s “next” node (essentially creating a linked list of nodes in every index space).

private int insertValue(int arrayPos, String element){//Checks for collisions with another SNode, and inserts the SNode into the appropriate spot in the array
 SNode targetNode = array[arrayPos];//What I want to be a reference to the node at the desired position in the array

 while (targetNode != null){//If an SNode already exists in that position, keeps iterating down until it gets to a non-existant SNode.
  targetNode = targetNode.getNext();//getNext is a method in my SNode that just returns a reference to that SNode's "nextNode" variable.
 }
  targetNode = new SNode(element);
  return arrayPos;
}//end insertValue

My problem is that after I run this method, it does not create a new node in the desired array position, even on the first time running when the array spot is null.

If I change the targetNode = new SNode(element); to array[arrayPos] = new SNode(element); it obviously inserts the SNode into the array just fine, so that leads me to believe that what is happening is the new SNode is being created under the variable targetNode, but that targetNode is not linked to the array position after it is instantiated. I assume it is essentially copying the data from the array position on line 2 into the variable, but then becomes its own separate entity.

So how do I have targetNode actually reference and affect the SNode? (That way way when I iterate down through the linked list of nodes in an already-occupied array space, targetNode is pointing to the correct one.)

NOTE: For the sake of simplicity I have left out the lines that use the setNext() method in SNode to link the previous node in the linked list to its next one.

Advertisement

Answer

You have a misconception. Neither variables nor array elements hold objects. They hold references to objects. Furthermore, variables and array positions are not themselves objects, and therefore there is no way in Java to have a reference to one. The closest you can come is to have a copy of the value that your variable or array position contains (a reference to an object).

Thus, this …

SNode targetNode = array[arrayPos];

… copies the value of array[arrayPos] into variable targetNode. If that value is non-null then the variable afterward refers to the same object that the array element does, but the object itself is not copied. And that’s fine and exactly what you want in that case, for when you walk the linked list with

targetNode = targetNode.getNext();

, you don’t want to modify array[arrayPos] or any of the nodes’ next refernces, for then you would lose elements of your linked list.

But you can’t have it both ways. When you ultimately find the position for the new SNode and perform this …

targetNode = new SNode(element);

… it does not record a reference to the new SNode wherever it was that you most recently copied targetNode‘s value from. It just puts the reference in targetNode.

What you want to do is find the last current node, if any, and assign to its next reference (or directly to the array element if it is initially null).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement