【问题标题】:How to instantiate MyList object [closed]如何实例化 MyList 对象
【发布时间】:2013-09-23 17:29:07
【问题描述】:

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

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

我不明白的主要部分是嵌套类 - 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
  }
}

【问题讨论】:

  • 如果不了解他们所知道的/熟悉的术语/等,就很难向某人解释某事。您有具体问题吗?跨度>
  • jedwards,我知道大量的 C++ 并且刚刚开始使用 Java。我试图为这个程序实现一个 main 实例化一个 MyList 对象并将值 1-10 放入其中。然后在顶部添加了一些数字并从任何地方删除了一些数字。但我意识到我并不是很了解这门课,所以需要帮助。
  • 根据您对每个人在做什么的理解,通过在每一行添加 cmets 来编辑您的帖子。
  • 稍微调整了问题以显示我真正不理解的内容以及一旦我理解了我想做什么。 Mark S 给出了很好的解释。

标签: java


【解决方案1】:

让我们从基础开始: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.

}

希望这能让您开始了解这里发生的事情,如果有任何问题不够清楚,请务必提出更多问题。

【讨论】:

  • 谢谢,这很清楚,绝对可以帮助我理解这一点。我将如何更改remove 方法以允许我从我想要的列表中的任何位置remove?例如,我将一个数字传递给remove,然后remove 将删除该值。
  • @RichieMiz - “我传递了一个数字”你的意思是像数组index这样的东西吗?如果是这样,我相信如果不至少实现iterator 接口并因此使MyClass iteratable 是无法完成的。如果您在现实生活中的应用程序中需要此类功能,请使用 Java Collections 之一。
  • 我也想把anInstance的内容打印出来,但不知道怎么做。
  • 要以有意义的方式打印对象,您需要覆盖其toString() 方法。然后就可以使用System.out.println()方法打印了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-05
  • 2018-09-06
相关资源
最近更新 更多