为什么类字段在方法执行后会更新它们的数据

huangapple go评论79阅读模式
英文:

Why does the class fields update their data after the method works

问题

请帮我。假设我有一个用于链表的Link类。然后有一个SortedList类,其中包含用第一个类创建的数据进行操作的方法。

public class Link {
    public long db;
    public Link next;

    public Link(long data){
        db = data;
    }

    public void displayLink(){
        System.out.println(db + " ");
    }
}

public class SortedList {    
    private Link first;  
    
    public SortedList(){  
        first = null;    
    }
    
    public void insert(long key){
        Link newLink = new Link(key);
        Link previous = null;
        Link current = first;
        
        while (current != null && key >= current.db){
            previous = current;
            current = current.next;
        }
        
        if (previous == null){
            first = newLink;
        } else {
            previous.next = newLink;
            newLink.next = current;
        }
    }
    
    public void displayList(){
        System.out.println("List (first-->last): ");
        Link current = first;
        
        while (current != null){
            current.displayLink();
            current = current.next;
        }
        
        System.out.println();
    }
}

insert方法使用了first字段。first字段将其数据传递给current字段。方法退出后,对current字段进行的更改不会影响first字段,但更改仍会在first字段中显示出来。

这是如何发生的,请您帮助。

英文:

Help me please.
Let's say I have a Link class for linked lists.
And there is the SortedList class where there are methods for working with data created by the first class.

public class Link {
public long db;
public Link next;
public Link(long data){
    db=data;
}
public void displayLink(){
    System.out.println(db+" ");
}
}


   public class SortedList {    
   private Link first;  
   public SortedList(){  
   first=null;    
   }
   public void insert(long key){
    Link newLink=new Link(key);
    Link previous=null;
    Link current=first;
    while (current!=null&&key>=current.db){
        previous=current;
        current=current.next;
    }
    if (previous==null){
        first=newLink;
    }else {
        previous.next=newLink;
        newLink.next=current;
    }

  }
public void displayList(){
    System.out.println("List (first-->last): ");
    Link current=first;
    while (current!=null){
        current.displayLink();
        current=current.next;
    }
    System.out.println();
}
  }

The insert method uses the first field.
The first field passes its data to the current field.
After the method exits with the current field, no changes are made to the first field, but the changes still appear in the first field.

How it happens, I pray you help

答案1

得分: 1

insert 开始时,我们将 current 设置为对 first 值的引用。这意味着,在此时,currentfirst 都指向相同的字段。

然后 while 循环从 first 开始迭代节点,直到我们到达列表末尾或者找到一个键小于 key 的节点。迭代过程通过更新 current 来跟随当前节点的 next 引用。

现在发生了让你困惑的部分:如果 firstnull(即第一次调用 insert),下一个操作通过给它赋新值来更新 first。现在,first 将指向 newLink 的值,这恰好是我们在 insert 顶部创建的节点。

最好拿起纸和笔,绘制一个包含所有变量列的表格,逐步执行算法,就像计算机一样。你也可以通过使用调试器做类似的事情,在方法开始处设置断点,然后“逐步执行”代码。

英文:

At the beginning of insert, we set current to be a reference to the value of first. This means that, at this point, both current and first are references to the same field.

The while loop then iterates over the nodes starting from first until we either hit the end of the list or a node whose key is less than key. The iteration happens by updating current to follow the next reference of the current node.

Now happens the part that confused you: if first was null (i.e. the first time we call insert, the next operation updates first by assigning it a new value. first will now refer to the value of newLink, which is precisely the node that we created at the top of insert.

It helps to take pen and paper, draw a table with columns for all variables, and go through the algorithm step by step, like a computer would. You can do something similar by using the debugger, set a break point at the beginning of the method, and “step through” your code.

huangapple
  • 本文由 发表于 2020年10月21日 04:13:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64452702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定