Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 链表(循环)_Java_Loops_Linked List - Fatal编程技术网

Java 链表(循环)

Java 链表(循环),java,loops,linked-list,Java,Loops,Linked List,我在执行链表循环时遇到问题。 我想尝试从用户那里输入ID和名称,但似乎最后输入的细节在整个链接过程中都被覆盖了。但是当我使用实例化(myLinkedList.insertFirstLink(“name”,id))时,它确实起作用了 这是我的密码 import java.io.*; import java.util.*; class Example { public static int ID; public static int ans;

我在执行链表循环时遇到问题。 我想尝试从用户那里输入ID和名称,但似乎最后输入的细节在整个链接过程中都被覆盖了。但是当我使用实例化
(myLinkedList.insertFirstLink(“name”,id))
时,它确实起作用了

这是我的密码

import java.io.*;
import java.util.*;

    class Example
    {
        public static int ID;
        public static int ans;
        public static String name;
        public Example next;

        public Example(int ID, String name)
        {
            this.ID = ID;
            this.name = name;
        }

        public void display()
        {
            System.out.println("ID: "+ID + " - " + name);
        }

        public String toString()
        {
            return name;
        } 

        public static void main(String args[]) throws IOException
        {   
            BufferedReader inpt = new BufferedReader(new InputStreamReader (System.in));

            int x = 0;
            LinkList myLinkedList = new LinkList();

            while(x<=2)
            {
            System.out.print("Enter Name: ");
            name = inpt.readLine();

            System.out.print("Input ID#: ");
            ID = Integer.parseInt(inpt.readLine());

            System.out.println();

            myLinkedList.insertFirstLink(name, ID);
            x++;
            }

         /* myLinkedList.insertFirstLink("Vishera", 2341);
            myLinkedList.insertFirstLink("Bulldozer", 1234);
            myLinkedList.insertFirstLink("Allendale", 3214);
            myLinkedList.insertFirstLink("Wolfdale", 4312); */

            myLinkedList.displayLink();

        }
    }

    class LinkList
    {
        public Example head;
        public Example tail;

        public LinkList()
        {
            head = null;
            tail = head;
        }

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

        public void insertFirstLink(String name, int ID)
        {
            Example newLink = new Example(ID, name);
            if(head==null)
            {
                head = newLink;
                tail = head;
            }
            else 
            {
                tail.next = newLink;
                tail = newLink;
            }
        }

        public void displayLink()
        {
            Example theLink = head;

            while(theLink != null)
            {
                theLink.display();

                System.out.println("The Next Link: " + theLink.next);

                theLink = theLink.next;

                System.out.println();

            }
        }
    }
import java.io.*;
导入java.util.*;
课例
{
公共静态int-ID;
公共静态INTANS;
公共静态字符串名;
其次是公众榜样;
公共示例(int-ID、字符串名称)
{
this.ID=ID;
this.name=名称;
}
公共空间显示()
{
System.out.println(“ID:+ID+”-“+name”);
}
公共字符串toString()
{
返回名称;
} 
公共静态void main(字符串args[])引发IOException
{   
BufferedReader Inputt=新的BufferedReader(新的InputStreamReader(System.in));
int x=0;
LinkList myLinkedList=新建LinkList();

而(x您的
示例类中有一个问题

首先,您没有创建实例变量,而是使用static创建了类变量。对于
创建实例变量
,请仅使用
private
关键字

已更正的代码

public class Example {
private int ID;
private int ans;
private String name;
public Example next;

public Example(int ID, String name) {
    this.ID = ID;
    this.name = name;
}

public void display() {
    System.out.println("ID: " + ID + " - " + name);
}

public static void main(String args[]) throws IOException {
    int userId;
    String userName;
    BufferedReader inpt = new BufferedReader(new InputStreamReader(
            System.in));

    int x = 0;
    LinkList myLinkedList = new LinkList();

    while (x <= 2) {
        System.out.print("Enter Name: ");
        userName = inpt.readLine();

        System.out.print("Input ID#: ");
        userId = Integer.parseInt(inpt.readLine());

        System.out.println();

        myLinkedList.insertFirstLink(userName, userId);
        x++;
    }

    /*
     * myLinkedList.insertFirstLink("Vishera", 2341);
     * myLinkedList.insertFirstLink("Bulldozer", 1234);
     * myLinkedList.insertFirstLink("Allendale", 3214);
     * myLinkedList.insertFirstLink("Wolfdale", 4312);
     */

    myLinkedList.displayLink();

}

public String toString() {
    return name;
}

}

class LinkList {
public Example head;
public Example tail;

public LinkList() {
    head = null;
    tail = head;
}

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

public void insertFirstLink(String name, int ID) {
    Example newLink = new Example(ID, name);
    if (head == null) {
        head = newLink;
        tail = newLink;
    } else {
        tail.next = newLink;
        tail = newLink;
    }
}

public void displayLink() {
    Example theLink = head;

    while (theLink != null) {
        theLink.display();

        System.out.println("The Next Link: " + theLink.next);

        theLink = theLink.next;

        System.out.println();

    }
}
}

调试您的代码,您将在不到3秒钟的时间内发现问题。为什么在示例类中将字段设置为静态?将它们设置为一个实例,它将按预期工作。@KevinBowersox每个
示例
对象都有一个
下一个
字段。请查看我的answer@ZouZou-我将其设置为静态,因为我要求用户输入放一些东西,这不是为了避免非静态变量吗?我尝试了你的代码,但出于某种原因,我得到了一个无限循环。非常感谢!这是我需要继续我的工作的东西。再次,谢谢。救了我。
Enter Name: a
Input ID#: 1

Enter Name: b
Input ID#: 2

Enter Name: c
Input ID#: 3

ID: 1 - a
The Next Link: b

ID: 2 - b
The Next Link: c

ID: 3 - c
The Next Link: null