Java 如何实例化MyList对象

Java 如何实例化MyList对象,java,Java,我在教科书“Java by Dissection”中看到了这段代码,但我不明白它到底做了什么。除了说它实现了一个嵌套类之外,教科书几乎没有解释任何关于这段代码的内容。但我想了解它的实际作用 我想理解这段代码的原因是因为我试图创建一个main,它将声明/实例化值为1-10的MyList类的对象。然后在顶部添加一些数字,并从我想要的任何地方删除一些数字。有人能帮我吗 我不理解的主要部分是嵌套类ListedElement public class MyList { private ListElem

我在教科书“Java by Dissection”中看到了这段代码,但我不明白它到底做了什么。除了说它实现了一个嵌套类之外,教科书几乎没有解释任何关于这段代码的内容。但我想了解它的实际作用

我想理解这段代码的原因是因为我试图创建一个main,它将声明/实例化值为1-10的MyList类的对象。然后在顶部添加一些数字,并从我想要的任何地方删除一些数字。有人能帮我吗

我不理解的主要部分是嵌套类ListedElement

public class MyList {
  private ListElement head, tail; //Forward declaration
  void add(Object value) {
    if (tail != null) {
      tail.next = new ListElement(value);
      tail = tail.next;
    }
    else {
      head = tail = new ListElement(value);
    }
  }
  Object remove() 
  {
    assert head != null; // don't remove on empty list
    Object result = head.value;
    head = head.next;
    if (head == null) { //was that the last?
      tail = null;
    }
    return result;
  }
  //Nested class needed only in the implementation of MyList
  private class ListElement {
    ListElement(Object value) {this.value = value;}
    Object value;
    ListElement next; //defaults to null as desired
  }
}

让我们从基础知识开始:MyList是一个类,它是Java中的一个代码单元。在该类中,您有:

  • 方法:添加、删除
  • 内部类:ListElement
  • 一些字段:head、tail,都是ListElement类型
在任何类中,“东西”通常发生在调用方法时。在这里,有两个,它们都按自己说的做

解释它的最佳方式可能是实际演示如何在代码中使用它:

public static void main(String[] args) {
    MyList anInstance = new MyList(); // creates an instance of the MyList class
    // note that at this point, the instance is created, but because there is no constructor method, the fields (head, tail) are both null

    String someValue = "A list element";
    anInstance.add(someValue); // add an element to the list
    // if you step through the add method, you'll see that the value coming in is a String ("A list element"), but nothing has been initialized yet (head, tail are both null)
    // So you'd go to the "else" bit of the logic in the add method, which initializes the head and tail element to the same object that you passed in.  So now your list contains one item ("A list element");

    String anotherValue = "Another value";
    anInstance.add(anotherValue); // add a second element to the list
    // now the path through the add method is different, because your head and tail elements have been initialized, so set the tail.next value to the new element, and then have tail be this new value.
    // so the head is "A list element" and the tail is "Another value" at this point, and the head's next field is "Another value" (this is the linking part of a linked list)

    // from here, you could add more elements, or you could remove elements.  The remove method is pretty straight forward as well -- it removes from the front of the list (note the part of the code that returns the head, and then updates the head value to point to the next item in the list.

}

希望这能让你开始了解这里发生的事情,如果其中任何一个问题不够清楚,一定要问更多的问题。

这是一个链接列表,如果不知道一点他们知道什么/他们熟悉的术语/等等,就很难向某人解释一些事情。你有具体的问题吗?jedwards,我知道大量的C++,而我只是从java开始。我试图为这个程序实现一个main,它实例化了一个MyList对象,并将值1-10放入其中。然后在顶部添加一些数字,并从任何位置删除一些数字。但我意识到我并不真正理解这门课,所以我想得到帮助。做一件事,根据你对每一行的理解,在每一行上添加评论,编辑你的帖子。稍微修改一下问题,以显示我真正不理解的内容,以及我理解后想做的事情。Mark S给了我一个很好的解释。谢谢,这非常清楚,肯定能帮助我理解这一点。如何更改
remove
方法以允许我从列表中的任何位置
remove
?例如,我将一个数字传递给
remove
,在该位置
remove
将删除该值。@RichieMiz-我传递一个数字”的意思是类似于数组索引的吗?如果是这样的话,我相信至少要实现接口,从而使
MyClass
,这是不可能的。如果您在实际应用程序中需要此类功能,请使用Java中的一种。我还想打印出
anInstance
的内容,但不确定如何执行。要以有意义的方式打印对象,您需要重写其方法。然后您可以使用方法打印它。