Java程序中打印出对象成员

Java程序中打印出对象成员

本文介绍了如何从有序的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);
 }
}

推荐答案


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程序中打印出对象成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:25