【问题标题】:I am getting the memory address from an arraylist, need info我从数组列表中获取内存地址,需要信息
【发布时间】:2013-09-21 05:11:15
【问题描述】:

我正在获取一个文本文件并填充一个数组列表。为了测试文件,我在继续之前将其打印出来。我只能看到内存地址,而不是文件中的实际信息。是否有一些简单且可能很明显的东西我遗漏了?

public class TriviaQuestion {

private String player;
private String category;
private String question;
private String answer;
private int score = 0;

/**
 * 
 */


public TriviaQuestion() {
    player = "unknown";
    category = "unknown";
    question = "unknown";
    answer = "unknown";
    score = 0;
}

public TriviaQuestion(String category, String question, String answer){

}
public TriviaQuestion(String player, String category, String question,
        String answer, int score) {
    super();
    this.player = player;
    this.category = category;
    this.question = question;
    this.answer = answer;
    this.score = score;
}


/**
 * @return the player
 */
public String getPlayer() {
    return player;
}


/**
 * @param player the player to set
 */
public void setPlayer(String player) {
    this.player = player;
}


/**
 * @return the category
 */
public String getCategory() {
    return category;
}


/**
 * @param category the category to set
 */
public void setCategory(String category) {
    this.category = category;
}


/**
 * @return the question
 */
public String getQuestion() {
    return question;
}


/**
 * @param question the question to set
 */
public void setQuestion(String question) {
    this.question = question;
}


/**
 * @return the answer
 */
public String getAnswer() {
    return answer;
}


/**
 * @param answer the answer to set
 */
public void setAnswer(String answer) {
    this.answer = answer;
}


/**
 * @return the score
 */
public int getScore() {
    return score;
}


/**
 * @param score the score to set
 */
public void setScore(int score) {
    this.score = score;
}







}

测试者

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class TriviaQuestionTester {

/**
 * @param args
 */
public static void main(String[] args) throws IOException {

     File triviaFile = new File("trivia.txt");

    Scanner triviaFile1 = new Scanner(triviaFile);
    String lastKnownCategory = "";

    ArrayList<TriviaQuestion> myList = new ArrayList<TriviaQuestion>();
    while (triviaFile1.hasNextLine()){
        String currentLine = triviaFile1.nextLine();
        if (!currentLine.isEmpty()) {
            TriviaQuestion currentQuestion = new TriviaQuestion(currentLine,     currentLine, currentLine);

            if (currentLine.endsWith("?")) {
                currentQuestion.setCategory(lastKnownCategory);
                currentQuestion.setQuestion(currentLine);
                currentQuestion.setAnswer(triviaFile1.nextLine());
            } else {
                currentQuestion.setCategory(currentLine);
                currentQuestion.setQuestion(triviaFile1.nextLine());
                currentQuestion.setAnswer(triviaFile1.nextLine());
                lastKnownCategory = currentLine;
            }
            myList.add(currentQuestion);
        }


    }
    triviaFile1.close();





    System.out.println(myList);

}

}

【问题讨论】:

  • 你看到的不是内存地址,而是哈希码。

标签: java file-io arraylist


【解决方案1】:

我缺少一些简单且可能很明显的东西吗?

是的 - 你没有在TriviaQuestion 中覆盖toString,所以你得到了default implementation from Object

Object 类的 toString 方法返回一个字符串,该字符串由对象作为其实例的类的名称、at 符号字符“@”和对象哈希码的无符号十六进制表示形式组成。换句话说,这个方法返回一个字符串等于:

getClass().getName() + '@' + Integer.toHexString(hashCode())

只需将此添加到TriviaQuestion

@Override public String toString() {
    return "Player: " + player + "; Category: " + category 
        + "; Question: " + question + "; Answer: " + answer
        + "; Score: " + score;
}

(或者在方法中使用String.format来做同样的事情。)

【讨论】:

  • 我应该删除 toString 方法吗?如果不是,我将如何骑过它?
【解决方案2】:

您需要覆盖TriviaQuestion 类中的toString 方法,以便以您想要的方式打印对象。 toString 方法的默认实现打印对象的内存表示。

注意:如果你不知道怎么写toString方法,那么可以利用eclipse等IDE为你生成toString方法。在日食中,

在编辑器中进入你的类,右键->源->生成 到字符串。

【讨论】:

  • 是的,这就是我在 Eclipse 中制作 toString 的方式。
  • @StevenEck 添加 toString 后,打印 TriviaQuestion 的对象或整个对象列表,应该以漂亮的形式显示。
  • 是的,它现在看起来很不错。现在我要做的就是弄清楚如何使用其中的标记为数组列表中的信息创建单独的数组列表。谢谢
猜你喜欢
  • 2013-10-29
  • 2010-12-13
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多