gpt4 book ai didi

java - 学习 LinkedList 和 Nodes,但我在 buildList 方法的 newNode 上不断收到 "cannot be resolved or is not in field"错误

转载 作者:行者123 更新时间:2023-11-30 06:13:50 25 4
gpt4 key购买 nike

这几天我一直在试图解决这个问题,但我所做的一切都是错误的。对于 buildList 方法中 newNode.next 上的 .next,我不断收到“无法解析或不在字段中”错误。对于 printList 方法中 current.next/current.data 上的 .next 和 .data,我也遇到同样的错误。我所拥有的是书上的内容,但它不想在 Eclipse 中工作。请帮忙...

package linkedList;

import java.util.*;
import org.w3c.dom.Node;

public class ListOne {
//This part needs various options:
//Build list
//clear list
//check if the list is sorted
//insert at head
static Scanner input = new Scanner(System.in);
public static Node head;
public int linkedListCount = 0;
//public static LinkedList<Integer> intList = new LinkedList<Integer>();

private class MyNode{
private int data;
private Node next;

public MyNode(int data){
this.data = data;
this.next = null;
}
}
//BUILD LIST
public void buildList(int value){
Node newNode = (Node) new MyNode(value);
newNode.next = head;
head = newNode;
}
//Clear the list
public void clearList(){
head = null;
}

public void printList () {
if(head == null){
return;
}
Node current = head;
while (current != null) {
// visit
System.out.println(current.data);
current = current.next;
} // traversal
} // printList


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

Here is the errors I am receiving. In method buildList, on newNode.next = "next cannot be resolved or is not a field." / In method printList, on current.data = "data cannot be resolved or is not a field." / In method printList, on current.next = "next cannot be resolved or is not a field."

最佳答案

我根本看不出使用 Node 接口(interface)有什么意义。只需使用 MyNode 即可:

package linkedList;

import java.util.*;
//import org.w3c.dom.Node; No need for this

public class ListOne {
// .....
public static MyNode head;
private class MyNode{
private int data;
private MyNode next;

public MyNode(int data){
this.data = data;
this.next = null;
}
}
//BUILD LIST
public void buildList(int value){
MyNode newNode = new MyNode(value);
newNode.next = head;
head = newNode;
}
// etc....
}

关于java - 学习 LinkedList 和 Nodes,但我在 buildList 方法的 newNode 上不断收到 "cannot be resolved or is not in field"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49662046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com