http://stackoverflow.com/questions/5607773/change-a-functions-arguments-values
To clarify, the bool variable does not change in the calling method. A new copy of the variable is created in the called method. You can change it as much as u like, but this var is different from the one in calling method. Yet, if your argument was a pointer, then any changes in the called method will directly affect the object in the calling method, since a new copy of the variable is not created in the called method.
http://stackoverflow.com/a/16513496
Java functions parameters are called by reference name, meaning that when you place an object as an function argument, the JVM copies the reference's value to a new variable, and passes it to the function as an argument. If you change the contents of the object, then the original object will change, but if you change the actual value of the reference, then these changes will be destroyed when the function ends.
Therefore, the following call to the sub function will not result a list with appropriate items deleted:
public static void test2() {
MySingleLinkedNode<Integer> list1 = createIntList(new int[]{1,1,1,3,4,6,6,7,7});
MySingleLinkedListUtils.deleteAllFromList(list1, 1);
list1.printRestOfList();
}
public static <T> void deleteAllFromList(MySingleLinkedNode<T> head, T data){
while (head.getNext() != null){
// 1. head is the one we are deleting, loop until we delete all such occurrence
if (head.getData().equals(data)){
head = head.getNext();
}
else{
break;
}
}
// 2. head is not the one we are deleting
MySingleLinkedNode<T> prevNode = head;
while (prevNode.getNext() != null){
MySingleLinkedNode<T> currNode = prevNode.getNext();
if (currNode.getData().equals(data)){
// we found the node to delete
prevNode.setNext(currNode.getNext());
}
prevNode = prevNode.getNext();
}
}