首先,这是家庭作业的一部分,因此请在回答时牢记这一点。
因此,我尝试使用通用 LinkedList 类,CustomerList 类继承自该类。下面的所有代码都是我编写的,不是作为作业的一部分给出的。
我已经编写了客户类型,可以成功运行。不需要该代码,因为问题与该类无关。它有 3 个 int 字段,以及一个打印其信息的方法。
public void printCustomerInfo() {
System.out.println([int field 1, 2, 3]);
}
这个问题与通用 LinkedList 类的 insert 方法(我相信)有关。它接收已确定类的对象,并将其插入列表中当前 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 类扩展了它,并将数据类型设置为 customer。 这是代码:
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;
}
即使这样也没有改变数据的空值。我知道这一定是由于没有正确理解 Java 泛型,因此我们将非常感谢您提供的任何指示(以及可供阅读的在线资源)。
最佳答案
这里有一个提示。如果您在父 LinkedList 类和子类中都声明数据成员,则子成员的版本将覆盖父类的版本。
关于java - 为什么插入返回后 LinkedList.data 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138557/