Reputation: 1067
So I have an array full of nodes called "SNode
s" (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.
Upvotes: 2
Views: 887
Reputation: 6336
As the other answers show, with the normal solution to this, you would test whether it is the first element in the array, and do something different in that case. That is, set the element directly in the array if it is the first one, walk the list if it is not.
A valid alternative approach would be to use a dummy element in the array, that is only used for its next field, and initialise the array with dummy elements at creation time. In your case, you could even give the dummy element an additional last
field, which would negate the need to walk the list to find the last element. Something like the following:
interface SNodeRef {
SNode getNext();
void setNext(SNode node);
}
class FirstNode implements SNodeRef {
SNodeRef getLast();
void setLast(SNodeRef);
....
FirstNode() { setLast(this); }
}
class SNode implements SNodeRef { ... }
private int insertValue(int arrayPos, String element) {
SNode newNode = new SNode(element);
FirstNodeRef ref = array[arrayPos];
ref.getLast().setNext(newNode);
ref.setLast(newNode);
}
FirstNode[] array = new FirstNode[10];
for (int i = 0; i < array.length; i++) {
array[i] = new FirstNode();
}
Alternatively, as it is a linked list that you are using, you could simply use the predefined LinkedList
data structure:
private int insertValue(int arrayPos, String element) {
array[arrayPos].addLast(element);
}
var array = new LinkedList<String>[10];
for (int i = 0; i < array.length; i++) {
array[i] = new LinkedList<String>();
}
Upvotes: 1
Reputation: 131326
Here targetNode
refers to the object referenced by array[arrayPos]
.
SNode targetNode = array[arrayPos];//What I want to be a reference to the node at the desired position in the array
But then when you write :
targetNode = targetNode.getNext();//getNext is a method in my SNode that just
you change the object referenced by the targetNode
variable. Now it refers to the next node of it.
Then when you do :
targetNode = new SNode(element);
You create a new object and assign it to the targetNode
variable but finally it is never associated to an existing node.
It doesn't assign to the next node a new node.
To do it you could write :
targetNode.setNext(new SNode(element));
Upvotes: 1
Reputation: 180073
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
).
Upvotes: 1