在编写一个演示链表的类的过程中,我没有得到我需要的结果。我编写了一个包含内部节点类的类,以及一个向列表添加名称和分数的插入方法,通过删除分数最低的人将列表限制为10。我还创建了一个测试GUI程序。当运行并输入insert命令时,列表显示我的类的名称(GamerList),然后是" @"和数字而不是欲望的名字和分数。我怀疑我的问题是在我的插入方法中的某个地方。任何反馈都会非常感激。
链接列表类
class GameList
{
//node class
private class Node
{
String name;
int score;
Node next;
//Node constructor
Node(String namVal, int scrVal, Node n)
{
name = namVal;
score = scrVal;
next = n;
}
//Constructor
Node(String namVal, int scrVal)
{
this(namVal, scrVal, null);
}
}
private Node first; //head
private Node last; //last element in list
//Constructor
public GameList()
{
first = null;
last = null;
}
//isEmpty method: checks if param first is empty
public boolean isEmpty()
{
return first == null;
}
public int size()
{
int count = 0;
Node p = first;
while(p != null)
{
count++;
p = p.next;
}
return count;
}
//Override toString
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.name);
p = p.next;
}
return strBuilder.toString();
}
public void insert(String name, int score)
{
Node node = new Node(name, score);
final int MAX_LIST_LEN = 10;
if(isEmpty())
{
first = node;
first.next = last;
}
else if(first.score <= node.score)
{
node.next = first;
first = node;
}
else
{
Node frontNode = first;
while(frontNode.score > node.score && frontNode.next != null)
{
frontNode = frontNode.next;
}
node.next = frontNode.next;
frontNode.next = node;
}
if(size() > MAX_LIST_LEN)
{
Node player = first;
for(int i = 0; i < 9; i++)
{
player = player.next;
}
player.next = null;
}
}
}
测试计划
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
This class is used to demonstrate
the operations in the GameList class.
*/
public class GameListGui extends JFrame
{
private GameList topGamers;
private JTextArea listView;
private JTextField cmdTextField;
public GameListGui()
{
topGamers = new GameList();
listView = new JTextArea();
cmdTextField = new JTextField();
// Create a panel and label for result field
JPanel resultPanel = new JPanel(new GridLayout(1,2));
resultPanel.add(new JLabel("Command Result"));
add(resultPanel, BorderLayout.NORTH);
// Put the textArea in the center of the frame
add(listView);
listView.setEditable(false);
listView.setBackground(Color.WHITE);
// Create a panel and label for the command text field
JPanel cmdPanel = new JPanel(new GridLayout(1,2));
cmdPanel.add(new JLabel("Command:"));
cmdPanel.add(cmdTextField);
add(cmdPanel, BorderLayout.SOUTH);
cmdTextField.addActionListener(new CmdTextListener());
// Set up the frame
setTitle("Linked List Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private class CmdTextListener
implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
String cmdText = cmdTextField.getText();
Scanner sc = new Scanner(cmdText);
String cmd = sc.next();
if (cmd.equals("insert")){
if (sc.hasNextInt())
{
// add index element
int score = sc.nextInt();
String name = sc.next();
topGamers.insert(name, score);
}
listView.setText(topGamers.toString());
pack();
return;
}
}
}
public static void main(String [ ] args)
{
new GameListGui();
}
}
答案 0 :(得分:3)
我认为你在某个地方有这样的电话:
topGamers.toString()
除非被覆盖,否则 toString
将打印出对象的类型和哈希码。对于我们这些不关心对象的类型和哈希码作为可打印表示的人来说,这两者都是完全无用的。
您要做的是实现String
的合理GameList
代表。
@Override
public String toString() {
// code
}
嗯......有一点需要注意 - GameList
只保留Node
,所以你必须从每个节点中提取信息。这意味着,对于Node
,您也会覆盖toString()
。
我将此部分作为练习留给读者(列表的拖网),但您最好在StringBuilder
中收集每个节点的信息,然后返回其toString()
值
答案 1 :(得分:2)
不,问题(在这个特定情况下,我没有检查你的列表是否真的按照它应该做的那样)是你没有为你的链表类定义toString
方法,所以它使用toString
中定义的默认object
,它打印类名和内存位置。