如何显示对象数组?

时间:2015-02-12 19:55:17

标签: java

如何显示方法 readAllExams(扫描仪)方法的对象数组?输出应该是这样的:

John Morgan 27'M'100

马库斯谢尔曼55'F'98

......................

但我只得到索引号。

class Exam {

    private String firstName;
    private String lastName;
    private int ID;
    private char examType;
    private int score;


    public Exam(String firstName, String lastName, int ID, char examType, int score)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.ID = ID;
        this.examType = examType;
        this.score = score;
    }

    public String getFirstName()
    {
        return this.firstName;
    }

    public String getLastName()
    {
        return this.lastName;
    }

    public int getID()
    {
        return this.ID;
    }

    public char getExamType()
    {
        return this.examType;
    }

    public int getScore()
    {
        return this.score;
    }

    public String toString()
    {
        return this.firstName + " " + this.lastName + " " + this.ID + " " + this.examType + " " + this.score;
    }

    public boolean equals(Exam object)
    {
        if(this.equals(object))
            return true;

        return false;
    }

}


import java.io.*;
import java.util.*;

class P2 {

    public static void main(String [] args) throws FileNotFoundException
    {

        Scanner data = new Scanner(new File("Exam.txt"));
        Exam[] readObjects = readAllExams(data);
        //Exam[] collateObjects = collateExams(readObjects);

        for(int i = 0; i < readObjects.length; i++)
        {
            System.out.println(i);
        }


    }

    public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
    {

        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted;
        int score = 0;

        int index = 0;

        Exam[] object = new Exam[50];

        while(s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            else 
                s.next();

            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

            object[index] = new Exam(firstName, lastName, ID, examTypeCasted, score);
            //System.out.println();
            index++;
        }
        readExam(s);
        return object;


    }

    public static Exam readExam(Scanner s)
    {
        String firstName = "";
        String lastName = "";
        int ID = 0;
        String examType = "";
        char examTypeCasted = 0;
        int score = 0;

        while (s.hasNext())
        {
            //Returns firtsName and lastName 
            firstName = s.next();
            lastName = s.next();

            //Returns ID number
            if(s.hasNextInt())
            {
                ID = s.nextInt();
            }
            //Returns examType which is 'M' or 'F'
            examType = s.next();
            examTypeCasted = examType.charAt(0);

            if(s.hasNextInt())
            {
                score = s.nextInt();
            }

        }
        Exam temp = new Exam(firstName, lastName, ID, examTypeCasted, score);
        return temp;
    }

2 个答案:

答案 0 :(得分:1)

您可以使用Arrays实用程序类(例如Arrays.toString(...))来显示整个数组的内容,但您的Exam类需要覆盖toString()

示例:

Exams[] exams = ...
System.out.println(Arrays.toString(exams));

答案 1 :(得分:0)

变化:

System.out.println(i);

System.out.println(readObjects[i]);