我从arraylist获取内存地址,需要信息

我正在获取一个文本文件并填充一个arraylist。为了测试文件,我在继续之前将其打印出来。我只能从文件中看到内存地址,而不能看到实际信息。有什么简单的东西,也许很明显我想念吗?

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);

}

}

回答:

有什么简单的东西,也许很明显我想念吗?

是的-

你没有覆盖toStringTriviaQuestion,所以你得到的默认实现Object

Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“

@”以及该对象的哈希码的无符号十六进制表示形式。换句话说,此方法返回的字符串等于:

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

只需将其添加到TriviaQuestion

@Override public String toString() {

return "Player: " + player + "; Category: " + category

+ "; Question: " + question + "; Answer: " + answer

+ "; Score: " + score;

}

(或String.format在方法内使用以执行相同的操作。)

以上是 我从arraylist获取内存地址,需要信息 的全部内容, 来源链接: utcz.com/qa/409923.html

回到顶部