我从数组列表中获取内存地址,需要信息 [英] I am getting the memory address from an arraylist, need info

查看:25
本文介绍了我从数组列表中获取内存地址,需要信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am taking a text file and filling an arraylist. To test the file I am printing it out before I move on. I am only able to see the memory address and not the actual info from the file. Is there something simple and probably obvious I'm missing?

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

}

}

推荐答案

是否有一些简单且可能很明显的东西被我遗漏了?

Is there something simple and probably obvious I'm missing?

是的 - 您没有在 TriviaQuestion 中覆盖 toString,因此您获得了 Object 的默认实现:

Yes - you haven't overridden toString in TriviaQuestion, so you're getting the default implementation from Object:

类 Object 的 toString 方法返回一个字符串,该字符串由对象是其实例的类的名称、at 符号字符@"和对象哈希码的无符号十六进制表示组成.换句话说,这个方法返回一个等于以下值的字符串:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

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

只需将此添加到TriviaQuestion:

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

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

(Or use String.format within the method to do the same kind of thing.)

这篇关于我从数组列表中获取内存地址,需要信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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