In the process of writing a class that demonstrates a linked list and I'm not getting the results I need. I have written a class that contains an inner node class as well as an insert method that adds a name and score to the list, limits the list to ten by removing the person with the lowest score. I have also created a test GUI program. When ran and type the insert command the list displays the name of my class (GamerList) followed by "@" and numbers rather than the desire name and score. I suspect my problem to be somewhere in my insert method. Any feedback would be much appreciated.
Linked List Class
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;
}
}
}
Test Program
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();
}
}
I figured you had a call like this somewhere:
topGamers.toString()
toString
, unless overridden, will print out the object's type and hash code. Both of which are completely useless to those of us not concerned with the object's type and hash code as a printable representation.
What you want to do is implement a sane String
representation of your GameList
instead.
@Override
public String toString() {
// code
}
Well...there's a slight caveat - the GameList
only holds Node
s, so you have to pull information out of each node too. That means, for Node
, you're overriding toString()
too.
I leave this portion as an exercise to the reader (the trawling of the list), but you would do well to gather the information of each node in a StringBuilder
, then return its toString()
value.
不,问题(在这种特定情况下,我没有检查您的列表是否确实应做的事情)是您尚未为链接列表类定义toString
方法,因此它使用在中定义的默认toString
object
,它打印类名称和内存位置。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.