rifle123
rifle123

Reputation: 259

Assign "this" to a reference variable in Java

I'm trying to used recursive method to complete the addLast method in a singly linked list, however, the code gives me a wrong output of list.size() = 2 and list.getFirst() = 5. The reason should be due to the line

SLList p=this;

It seems changing p reference changes "this" reference as well, which is not so logic to me. Could anyone give some details about this? Thx

public class SLList {
public class IntNode {
    public int item;
    public IntNode next;
    public IntNode(int i, IntNode n) {
        item = i;
        next = n;
    }
}

private IntNode first;

public SLList(int x) {
    first = new IntNode(x, null);
}

/** Adds an item to the front of the list. */
public void addFirst(int x) {
    first = new IntNode(x, first);
}

/** Retrieves the front item from the list. */
public int getFirst() {
    return first.item;
}

/** Adds an item to the end of the list. */
public void addLast(int x) {

    SLList p = this;
    if (p.first. next == null) {
        p.first.next = new IntNode (x, null);
    }

    else {
        p.first = p.first.next;
        p.addLast(x);
    }

}

/** Returns the number of items in the list using recursion. */
public int size() {
    /* Your Code Here! */
    SLList p = this;
    if (p.first == null) {
        return 0;
    }
    else if (p.first.next == null){
        return 1;
    }

    else {
        p.first = p.first.next;
        return 1 + p.size();
    }
}

public static void main (String[] args) {
    SLList list=new SLList (5);
    list.addFirst(10);
    list.addFirst(15);
    list.addLast(17);
    System.out.println(list.getFirst());
    System.out.println(list.size());

}

}

Upvotes: 0

Views: 587

Answers (4)

swapnil
swapnil

Reputation: 230

Problem in your implementation is addLast and size method are changing the value of field variable first.

It don't matter whether you assignthis to some variable or use directly. Because assigning this to some variable does not create new this object but assign's reference to that variable.

So you should first copy value of first field variable to some local variable then iterate on it.In this way your first will not change.

Hint: Don't change the first variable reference. Your addLast() and size() changes value of first which is wrong. Problem is in this line. p.first = p.first.next;

Upvotes: 0

Stephen C
Stephen C

Reputation: 719229

The problem is nothing to do with the assignment of this. Nothing can change this. Period.

(But things can change the state of the object that this refers to.)

The real problem is in your implementation of the size method. Your size method is causing the list to change. It shouldn't. In your case, the change causes:

  • the size() method to return the wrong value
  • subsequent getFirst() calls to return the wrong value.

I won't say exactly where the bug, but you should be able to spot it yourself by a process of elimination. (Or if that fails, use a debugger and try to observe where the list is changing.)

Upvotes: 4

Renuka
Renuka

Reputation: 146

SLList p = this;

p reference to the same SLList object. if you make any changes to 'p' then it will also happened to 'this', becuase of reference type (not value type).

Here in the statement

p.first = p.first.next;

the reference to the first is changed when you call 'addLast' method. You loss the reference to the first item.

If you remove the line

list.addLast(17);

in main method you will see the correct answer. The problem is with this method.

Change the method as follow and add the new method below.

/** Adds an item to the end of the list. */
public void addLast(int x) {
    addLast(x, this.first);
}

private void addLast(int x, IntNode node){
    if(node.next == null){
        node.next = new IntNode (x, null);
    }else {
        node = node.next;
        addLast(x, node);
    }
}

Then you will not lose the reference to first item and now it works fine,

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83557

There are bigger problems with your algorithms than you think. size() is incorrect. You can fix this if you realize that you need to count the number of IntNode objects in the list. Similarly all other methods need to manipulate IntNode objects.

Upvotes: 1

Related Questions