Java链表,don';我不明白

Java链表,don';我不明白,java,linked-list,stack,Java,Linked List,Stack,所以我一直在为即将到来的java考试学习,我遇到了一些我从未完全理解的东西。如果有人能给我解释一下,我将非常感激 好的,我遇到的问题是理解为什么下面的AddToEnd方法有效。在我看来,所有temp都是一个IntNode,表示列表中的最后一个元素,那么为什么通过修改temp,原始列表会发生变化呢?提前感谢您的帮助 public class IntList { private IntNode front; //first node in list //-------------------

所以我一直在为即将到来的java考试学习,我遇到了一些我从未完全理解的东西。如果有人能给我解释一下,我将非常感激

好的,我遇到的问题是理解为什么下面的AddToEnd方法有效。在我看来,所有temp都是一个IntNode,表示列表中的最后一个元素,那么为什么通过修改temp,原始列表会发生变化呢?提前感谢您的帮助

public class IntList {

  private IntNode front; //first node in list

//-----------------------------------------
// Constructor. Initially list is empty.
//-----------------------------------------
  public IntList() {
    front = null;
  }

//-----------------------------------------
// Adds given integer to front of list.
//-----------------------------------------
  public void addToFront(int val) {
    front = new IntNode(val,front);
  }

//-----------------------------------------
// Adds given integer to end of list.
//-----------------------------------------
  public void addToEnd(int val) {
    IntNode newnode = new IntNode(val,null);

//if list is empty, this will be the only node in it
    if (front == null)
      front = newnode;

    else {
//make temp point to last thing in list
      IntNode temp = front;

      while (temp.next != null)
        temp = temp.next;

//link new node into list
      temp.next = newnode;
    }
  }

//*************************************************************
// An inner class that represents a node in the integer list.
// The public variables are accessed by the IntList class.
//*************************************************************
  private class IntNode {
    public int val; //value stored in node

    public IntNode next; //link to next node in list

//------------------------------------------------------------------
// Constructor; sets up the node given a value and IntNode reference
//------------------------------------------------------------------
    public IntNode(int val, IntNode next) {
      this.val = val;
      this.next = next;
    }
  }
}
在我看来,所有temp都是一个IntNode,表示列表中的最后一个元素

我怀疑你意识到这是你的错误,但这是一个很难理解的概念

事实上,temp指向列表中的最后一个元素


当代码显示
temp.next=newNode
时,您实际上是在将列表中最后一个条目的
next
引用指向新条目-这正是您想要的。

好的,我明白您的意思。我原以为这些规则只适用于“静态”环境,但我猜不会。事实并非如此——几乎所有数据结构都依赖于这种引用形式——它对每个集合都至关重要,几乎在任何地方都会使用。我想不起我使用过的任何一种语言都没有将
引用作为核心功能的概念。甚至连FORTRAN都有一个。实际上,我对
COBOL
Basic
没有把握。