我是编程新手,遇到了访问问题。 目前我尝试练习 LinkedList - 通过自己创建一个然后使用它。
我在同一目录中有 3 个类 - 自定义“MyLinkedList”,另一个自定义类,我将其称为“Month”和“Main”。我的想法是创建 12 个 Month 对象类,因为每个对象都有 3 个字段并将它们添加到 LinkedList - 我做到了。现在我有问题,当我想检索某个对象字段的某个值时。拿到了Object的地址,却想不通,怎么才能到达这个地址后面的值。
示例 - 我创建了一个包含 12 个节点的 MyLinkedList,并在每个节点中放置了一个类型为 Month 的对象。稍后我想让我们说获取名为“季节”的三月(类型为月的对象)的字段,该字段存储在我的链表中。
我做什么 - 访问我的 LinkedList 中名为“item”的字段,该字段包含一个类型为 Month 的对象(对象的地址)。现在我想不通,我该如何进一步,到达这个地址后面的对象并检索该对象的字段保留的值。
我的理解是 - 1)我在堆栈中有一个名为“myList”的引用变量,它包含堆中“MyLinkedList”类型的对象的地址。 2) 内存中的位置,对应于这个地址,保存着 12 个“节点”的另外 12 个地址。 3)每个节点(一块内存),持有一个对象“月”的地址。 4) 内存中与Month 的地址相对应的地方保存了3 个字段(Object 的) 的地址。 5)在最后一个地址(字段的)后面放置了所需的值。所以我只能达到第 3 步,但无法继续。你会建议如何进一步进行。
我放了3种方法的一些代码:
这是我的代码:
public class MyLinkedList {
private class Node{
Object item;
Node next;
Node(Object item){
this.item = item;
this.next = null;
}
Node(Object item, Node next){
this.item = item;
this.next = next;
}
}
private Node head;
private Node tail;
private int count;
public void addNode(Object element){
Node newNode = new Node(element);
if(head == null){
head = newNode;
head.next = tail;
tail = head;
}
else {
tail.next = newNode;
tail = newNode;
}
count++;
}
public void printList(){
Node currentNode = head;
while(currentNode != null){
if(currentNode.next != null){
System.out.print(currentNode.item + ", ");
}
else {
System.out.println(currentNode.item + ";");
}
currentNode = currentNode.next;
}
}
}
public class Month {
private String season;
private int length;
public int index;
public Month(int index){
this.index = index;
}
public String getSeason(){
return season;
}
public void setSeason(String season){
this.season = season;
}
public int getLength(){
return length;
}
public void setLength(int length){
this.length = length;
}
}
public class Main {
public static void main(String[] args) {
Month January = new Month(0);
Month February = new Month(1);
Month March = new Month(2);
/*
....more of the same here
*/
January.setLength(31);
February.setLength(28);
March.setLength(31);
/*
....more of the same here
*/
January.setSeason("Winter");
February.setSeason("Winter");
March.setSeason("Spring");
/*
....more of the same here
*/
MyLinkedList myList = new MyLinkedList();
myList.addNode(January);
myList.addNode(February);
myList.addNode(March);
/*
....more of the same here
*/
System.out.println("Initially created list:");
myList.printList();
System.out.println();
}
答案 0 :(得分:1)
由于 item
在 Node
类中属于 Object
类型,因此您需要在通过定义的 getter 方法访问任何实例字段之前将其强制转换为 Month
在 Month
类中。
以下代码显示了如何访问链表每个节点上的 Month
对象的实例字段的示例。
public void printList(){
Node currentNode = head;
while(currentNode != null){
Month m = (Month) currentNode.item;
System.out.println(m.getSeason()); // print season
System.out.println(m.getLength()); // print length
currentNode = currentNode.next;
}
}
注意:如果您将 item
类中的 Node
的类型更改为 Month
,那么您不需要强制转换 {{1} } 到 currentNode.item
,然后再访问其实例字段。
或者,您可以覆盖 Month
方法,该方法返回 toString()
类实例的字符串表示形式。这样做将允许您将 Month
类的实例传递给 Month
以打印该实例的详细信息。
System.out.println()
现在,您可以这样做:
public class Month {
...
@Override
public String toString() {
return "Season: " + this.season + "\nLength: " + this.length;
}
}
public void printList(){
Node currentNode = head;
while(currentNode != null){
Month m = (Month) currentNode.item;
System.out.println(m); // print season and length
currentNode = currentNode.next;
}
}
- 此语句将打印在 System.out.println(m);
类中重写的 toString()
方法返回的任何内容。
同样,如果您将 Month
类中的 item
类型更改为 Node
,则不需要将 Month
转换为 currentNode.item
。在这种情况下,你可以这样做:
Month
答案 1 :(得分:1)
首先,如果您使用像 MyLinkedList
这样的泛型 MyLinkedList<Month>
,您将能够立即从这种泛型列表中获取类型为 Month
的对象,而无需进行任何强制转换。
在您的特定情况下,您必须像这样将 Object
类的对象显式转换回 Month
类:Month m = (Month)obj
,其中 obj
是您的示例项目从MyLinkedList
获取。
答案 2 :(得分:1)
如果只是为了打印目的,我建议为 toString()
类创建一个 Month
函数,然后在您的 printList()
方法中调用它。
toString
类的 Month
方法,由 Intellij 生成:
@Override
public String toString() {
return "Month{" + index + ", " + season + ", " + length + '}';
}
但话说回来,您的 LinkedList 包含节点,因此您也必须为 toString()
类创建 Node
方法:
@Override
public String toString() {
return item.toString();
}
当您完成所有这些操作后,只需修改您的 printList()
方法:
public void printList(){
Node currentNode = head;
while(currentNode != null){
if(currentNode.next != null){
// call toString function
System.out.print(currentNode.toString() + ", ");
} else {
System.out.println(currentNode.toString() + ";");
}
currentNode = currentNode.next;
}
}