关于显示数据的Java LinkedList
我已经创建了一个库存管理应用程序,用户可以在其中添加、查看和删除库存数据。当用户键入A时,程序将提示用户输入数据。如果用户输入D,程序将在清单中显示数据。我使用LinkedList来实现这一点。我对这个很陌生。我能够提示用户输入数据,但无法显示它。代码上有一条红线。我哪里出错了?如果我问的方式不对,我很抱歉。请纠正我 Main.java关于显示数据的Java LinkedList,java,linked-list,Java,Linked List,我已经创建了一个库存管理应用程序,用户可以在其中添加、查看和删除库存数据。当用户键入A时,程序将提示用户输入数据。如果用户输入D,程序将在清单中显示数据。我使用LinkedList来实现这一点。我对这个很陌生。我能够提示用户输入数据,但无法显示它。代码上有一条红线。我哪里出错了?如果我问的方式不对,我很抱歉。请纠正我 Main.java public static void main(String[] args) { Scanner sc = new Scanner(System
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = 0;
LinkedList myList = new LinkedList();
while(t != 1)
{
System.out.print("I, A, D");
char input = sc.next().charAt(0);
if(input == 'A')
{
System.out.print("Please enter item id: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Please enter item name: ");
String name = sc.nextLine();
System.out.print("Please enter item type: ");
String type = sc.nextLine();
System.out.print("Please enter item price: ");
double price = sc.nextDouble();
sc.nextLine();
Item I1 = new Item(id, name, type, price);
myList.addItemToFront(I1);
}
else if(input == 'D')
{
myList.DisplayItem(); //at this point, theres red line which i dont know why?
}
}
LinkedList.java
class LinkedList {
private Node head; // first node in the linked list
private int count;
public int getCount() {
return count;
}
public Node getHead() {
return head;
}
public LinkedList() {
head = null; // creates an empty linked list
count = 0;
}
public void addFront(int n) {
Node newNode = new Node(n);
newNode.setLink(head);
head = newNode;
count++;
}
public void deleteFront() {
if (count > 0) {
head = head.getLink();
count--;
}
}
public void addItemToFront(Item I1)
{
Node itemNode = new Node(I1);
itemNode.setLink(head);
head = itemNode;
}
public void DisplayItem(Node head)
{
if(head == null)
{
return;
}
Node current = head;
while (current != null)
{
System.out.println(current.data.toString());
current = current.getLink();
}
System.out.println(current);
}
public int length(Node head)
{
if(head == null)
{
return 0;
}
int count = 0;
Node current = head;
while(current != null)
{
count++;
current = current.getLink();
}
return count;
}
Node.java
public class Node {
Object data;
private Node link;
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLink() {
return link;
}
public void setLink(Node link) {
this.link = link;
}
public Node(Object data) {
this.data = data;
this.link = null;
}
}
示例输出(程序应如何运行示例)
正如我在LinkedList.java类中看到的,您的delete方法期望在迭代之前将节点头传递给它。然后需要将头部节点传递给显示方法
如果您将正确的代码放在Main.java中,我认为这是编译器错误。链表类中的display方法接受node类型的参数,即head。您需要从main方法传递该参数,然后它就可以工作了。 而不是:myList.DisplayItem(); 执行此操作:myList.DisplayItem(head); 现在,如果我们查看LinkedList类,您已经将“head”作为一个私有变量,或者将其公开,以便您可以从主类访问它,或者您可以从DisplayItem(Node head)方法中删除该参数,因为您可以使用head作为全局变量,然后在DisplayItem方法中对其进行迭代。 因此,最好将参数从方法定义中删除,它会起作用
public void DisplayItem() {
if (head == null) {
return;
}
Node current = head;
while (current != null) {
System.out.println(current.getNodeItem().toString());
current = current.getLink();
}
}
使用此方法“但无法显示”-如果您完全描述了输入和预期输出,将有所帮助。对不起。这是很新的。我相应地编辑了帖子。你可能还想发布Node类。还有一种建议的做法是,通过执行以下
类LinkedList
并使节点类更通用,从而使LinkedList类具有类型安全性。@J.Lee不确定为什么LinkedList
的元素需要具有可比性。这似乎是混淆了关注点。这不是必要的,但当您需要通过检查该值从链表中删除某个元素时,它会有所帮助。假设我想从整数链表中删除数字10linkedList.remove(10)
,在我看来,访问compareTo(T数据)
会有所帮助。但是我同意你的观点,它不一定非得是可比较的,不需要向DisplayItem传递任何参数。LinkedList对象具有头值,DisplayItem可以直接访问该值。确定。我明白你的意思。但当我加上head时,它仍然给了我一条红线。它问我应该创建变量头,我不明白为什么?头不应该从链表中检索吗?哈金巴胡里:你到底面临什么问题?在哪一行。我只看到DisplayItem函数问题。@IfOnly Ok。很抱歉给你带来了困惑。我想显示链接列表中的项目,我在main.java中键入它。else if(input='D'){myList.DisplayItem(head);}但是我不明白为什么在head下面有红色下划线?我需要声明吗?@HakimBajuri:head变量不是缅因对象的一部分。它是LinkedList对象的局部变量,您在其中编写了DisplayItem方法。您不需要将参数传递给此方法。您可以直接访问head变量。请参见下面修改的DisplayItem方法。我希望这是清楚的。谢谢你指出我的错误。你能给我解释一下为什么这很重要吗?我认为方法就是main在不考虑代码位置的情况下检索的方法?您的建议肯定会对我有所帮助。java类中可能有许多重载方法,例如DisplayItem()、DisplayItem(节点头)、DisplayItem(节点头、要出现的字符串)等。JVM必须确切地知道您希望从客户机代码中调用什么,因此方法的签名很重要。希望这能让你更清楚。谢谢你的详细解释!是的,它有效。非常感谢!
public void DisplayItem() {
if (head == null) {
return;
}
Node current = head;
while (current != null) {
System.out.println(current.getNodeItem().toString());
current = current.getLink();
}
}