如何从有序的LinkedList Java程序中打印出对象成员? [英] How to Print Out Object Members from an Ordered LinkedList Java Program?

查看:119
本文介绍了如何从有序的LinkedList Java程序中打印出对象成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
WordList对象管理WordItem对象的链接列表.添加到WordList的WordItem根据其序列号放置在列表中.例如,可以使用一组WordItem对象04little 02had 05lamb 01Mary 03a将押韵玛丽有一只小羊羔"添加到WordList中,在这种情况下,将对WordList中的WordItem对象进行排序

01Mary 02had 03a 04little 05lamb

我在运行代码时消息对话框返回的字符串有问题,因为它只显示"WordList @ 55933b00",而不是WordItem的字符串顺序,并在它们之间按顺序隔开.
此项目使用3个类:

WordItem类别:

Background:
A WordList object manages a linked list of WordItem objects. WordItems added to a WordList are placed in the list based on their sequence number. For example, the rhyme "Mary had a little lamb" might be added to a WordList using the set of WordItem objects 04little 02had 05lamb 01Mary 03a in which case the WordItem objects in the WordList would be ordered

01Mary 02had 03a 04little 05lamb

I''m having problem with the string that the message dialog returns when I run the code, because it just shows "WordList@55933b00" instead of the string sequence of WordItems concatenated with a space between them in order.
There are 3 classes that this project use:

WordItem Class:

public class WordItem
{
  private int sequence;
  private String word;
  
  public WordItem(String seq)
  {
    sequence =  Integer.parseInt(seq.substring(0, 2));
    word = seq.substring(2);
  }
  
  public int getSequence()
  {
    return sequence;
  }
  
  public String getWord()
  {
    return word;
  }
  
  public String toString()
  {
    return sequence + word;
  }
}



WordList类别:



WordList Class:

import java.util.LinkedList;
import java.util.ListIterator;

public class WordList
{
  private LinkedList list;
  
  public WordList()
  {
    list = new LinkedList();
  }
  
  public void addWordItem(WordItem item)
  {
    ListIterator iter = list.listIterator();
    if (list == null)
      list.addLast(item);
    else
    {
      while (iter.hasNext())
      {
        if (iter.next().getSequence() > item.getSequence())
        {
          iter.previous();
          iter.add(item);
          return;
        }
      }
    }
  }
  
  public LinkedList getList()
  {
    return list;
  }
  
  public String toStirng()
  {
    String str = "";
    for (WordItem item : list)
    {
      str += item.getWord() + " ";
    }
    return str;
  }
}



以及由教员&所提供的WordListTest.不允许修改:



And the WordListTest which is provided by the instructor & is not allowed to be modified:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class WordListTest
{
 public static void main(String[] args) throws FileNotFoundException
 {
  String[] choices = {"numbers.txt", "scrambledlimerick.txt", "Quit"};
        while (true) {
            int response = JOptionPane.showOptionDialog(
                               null                       // Center in window.
                             , "Choose file to use or Quit."   // Message
                             , "Create Word List"         // Title in titlebar
                             , JOptionPane.YES_NO_OPTION  // Option type
                             , JOptionPane.PLAIN_MESSAGE  // messageType
                             , null                       // Icon (none)
                             , choices                    // Button text as above.
                             , "Not used"                 // Default button's label
                           );
            //... Use a switch statement to check which button was clicked.
            switch (response) {
                case 0: case 1:
                    createWordList(choices[response]);
                    break;
                case 2: case -1: //... Both the quit button (2) and the close box(-1) handled here.
                    System.exit(0);     // It would be better to exit loop, but...
                default: //... If we get here, something is wrong.  Defensive programming.
                    JOptionPane.showMessageDialog(null, "Unexpected response " + response);
            }
        }
 }

 private static void createWordList(String fileName) throws FileNotFoundException
 {
  WordList list = new WordList();
  Scanner in = new Scanner(new File(fileName));
  while(in.hasNext())
   list.addWordItem(new WordItem(in.next()));
  JOptionPane.showMessageDialog(null, list);
 }
}

推荐答案

在此处执行任何操作-输出仅表示列表引用.
因此,您应该调用list.toString().
Whatever you do there - the output just represents a list reference.
So you should call for list.toString().


事实证明,我是个白痴,因为问题是我在WordList中拼写了toString()方法,所以它甚至没有被调用.如您所见,我在"public String toStirng()"上方有个白痴,这使我无休止地测试了该程序,因为我没有抓住它.这给了我作为程序员的宝贵经验.记住要进行拼写检查,以检查所有方法名称是否正确拼写.无论如何,正确的解决方案如下:

WordList已更正:

Well turns out, I''m a idiot because the problem was I misspelled the toString() method in WordList, so it was not even getting called. I had as you can see above "public String toStirng()" like an idiot, which caused me to test this program endlessly because I didn''t catch it. This has taught me a valuable lesson as a programmer. Remember to do a spell checker that checks to make sure all your method names are spelled correctly. Anyway the correct solution is below:

WordList Corrected:

import java.util.LinkedList;
import java.util.ListIterator;

public class WordList
{
  private LinkedList list;
  
  public WordList()
  {
    list = new LinkedList();
  }
  
  public void addWordItem(WordItem item)
  {
    if (list.size() == 0)
      list.addLast(item);
    else
    {
      ListIterator iter = list.listIterator();
      while (iter.hasNext())
      {
        if (iter.next().getSequence() > item.getSequence())
        {
          iter.previous();
          iter.add(item);
          return;
        }
      }
      iter.add(item);
    }
  }
  
  public LinkedList getList()
  {
    return list;
  }
  
  public String toString()
  {
    String str = "";
    for (WordItem item : list)
    {
      str += item.getWord() + " ";
    }
    return str;
  }
}


这篇关于如何从有序的LinkedList Java程序中打印出对象成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆