我在为Linked List做一个循环时遇到了麻烦。
我想尝试从用户输入ID和Name,但似乎最后输入的细节正在覆盖整个链接事物。但是当我使用实例化(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();
}
}
}
谢谢!
答案 0 :(得分:0)
Example class
首先,您还没有创建实例变量并使用static创建了类变量。 creating instance variable
仅使用private
关键字。
<强> Corrected Code
强>
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();
}
}
}
进行上述更改后,
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