【问题标题】:Why is the LinkedList.data null after returning from an insert?为什么从插入返回后 LinkedList.data 为空?
【发布时间】:2013-06-16 23:06:57
【问题描述】:

首先,这是家庭作业的一部分,所以在回答时请记住这一点。

所以我尝试使用一个通用的 LinkedList 类,CustomerList 类继承自该类。下面的所有代码都是我写的,不是作业的一部分。

我已经编写了客户类型,它可以成功运行。不需要该代码,因为问题与该类无关。它有 3 个 int 字段,以及一个打印它的信息的方法。

    public void printCustomerInfo() {
    System.out.println([int field 1, 2, 3]);
    }

问题与通用 LinkedList 类的插入方法(我相信)有关。它接收已确定类的对象,并将其插入到当前 LinkedList 对象前面的列表中。 它通过创建当前 LinkedList 对象的副本,将其设置为 nextList,然后将当前 LinkedList 对象的数据修改为给定的 dataIn 来实现这一点。 下面是 LinkedList 类的代码:

public class LinkedList<T> {
    T data;
    protected LinkedList<?> nextList;

    public LinkedList() {
        data = null;
        nextList = null;
    }

    public boolean isEmpty() {
        return (null == nextList);
    }

    public LinkedList<?> getNextList() {
        return nextList;
    }

    public void insert(T dataIn) {
        System.out.println("dataIn passed to insert method: \t" + dataIn);
        LinkedList<T> tempList = new LinkedList<T>();
        tempList.data = this.data;
        tempList.nextList = this.nextList;
        this.nextList = tempList;
        this.data = dataIn;
        System.out.println("data field of current object: \t\t" + data);
    }

    @SuppressWarnings("unchecked")
    public T delete() {
        T tempDump = data;
        data = (T) nextList.data;
        nextList = nextList.nextList;
        return tempDump;
    }

    public void printInfo() {
        if (isEmpty()) {
            System.out.println("-END-");
        } else {
            System.out.println(data);
            nextList.printInfo();
        }
    }
}

CustomerList 类对其进行了扩展,并将数据类型设置为客户。 代码如下:

public class CustomerList extends LinkedList<Customer> {
    Customer data;

    public void printInfo() {
        if (isEmpty()) {
            System.out.println("-END-");
        } else {
            data.printCustomerInfo();
            nextList.printInfo();
        }
    }

}

最后是测试对象:

public class GeneralTesting {
    public static void main(String[] args) throws InterruptedException {
        // Test LinkedList class
        System.out.println(" - Create CustomerList and test methods");
        CustomerList rList = new CustomerList();

        System.out.println(" - Create a customer to store in the list");
        Customer dude = new Customer(10, 65);
        dude.setTimeServed(120);

        System.out.println("proof that customer object exists: \t" + dude);

        System.out.println(" - Insert customer into the list");

        System.out.println("---method call: insert---");
        rList.insert(dude);
        System.out.println("---method end: insert----");

        System.out.println("data in the list after return: \t\t" + rList.data);
    }
}

这是控制台打印的内容:

 - Create CustomerList and test methods
 - Create a customer to store in the list
proof that customer object exists:  assignment3.Customer@3c250cce
 - Insert customer into the list
---method call: insert---
dataIn passed to insert method:     assignment3.Customer@3c250cce
data field of current object:   assignment3.Customer@3c250cce
---method end: insert----
data in the list after return:  null

据我所知/理解,这是一个范围界定问题。也许我将变量分配在方法解析时被忽略的级别。我之前遇到过类似的问题,并且能够解决它,但无法弄清楚。不幸的是,我的教授接下来几天不在城里,所以我在这里寻求帮助。

我只是尝试创建一种方法,将数据字段简单地设置为传入的对象:

public void setData(T dataIn) {
    this.data = dataIn;
}

即使这样也没有从 null 改变数据。我知道这一定是由于没有正确理解 Java 泛型,因此非常感谢您提供任何指点(以及在线阅读资源)。

【问题讨论】:

    标签: java generics linked-list


    【解决方案1】:

    这里有一个提示。如果您在父类 LinkedList 类和子类中都声明数据成员,则子成员将覆盖父类的版本。

    【讨论】:

    • 当然!因为 LinkedList 类使用泛型,所以不需要覆盖父级的数据属性。 LinkedList 类的扩展通过“LinkedList”表达式定义类型。我原本以为“客户数据”将充当类型定义。相反,它只是隐藏了想要的数据字段,并使其无法被外部函数访问。因此,数据字段并没有像我最初想到的那样被更改为空,而是被另一个为空的数据字段隐藏了。非常感谢!
    • 很好的推理!我想让你从正确的方向开始,因为你说这是家庭作业。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2015-10-23
    相关资源
    最近更新 更多