如何创建和返回一个对象数组?

时间:2015-02-10 00:03:23

标签: java arrays

我是对象/类概念的新手,并且已经在应用它们方面遇到了困难。可以请sombody解释/只显示下面的方法如何创建和返回一个Exam对象数组?我需要从传递给方法的文本文件中获取数据。提前致谢!我真的很感激!

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

public static Exam[] readAllExams(Scanner s)
{
         Exam[] arrayList = null;
    String firstName = "";
    String lastName = "";
    int ID = 0;
    String examValue = "";
    int score = 0;

    while(s.hasNext())
    {
        if(s.hasNextLine())
        {
            firstName = s.next();
            lastName = s.next();
        }
        else if(s.hasNextInt())
        {
            ID = s.nextInt();
        }
        else if (s.hasNextLine())
        {
            examValue = s.nextLine();
        }
        else if (s.hasNextInt())
        {
            score = s.nextInt();
        }

    }

    arrayList = {firstName, lastName, ID, examValue, score};
    return arrayList;
}

2 个答案:

答案 0 :(得分:1)

我假设您的输入是每行一次检查以及此格式的每一行:

firstName lastName ID examType score

所以可能的输入是:

firstName lastName 1 examType 2
Chuck Norris 42 Roundhousekicking 100
Sponge Bob 43 Burgermaking 50

(注意,扫描仪不区分空格和换行符,但只要每行都具有上述格式,这都无关紧要。此外,我不会处理输入中的错误而只是让扫描仪如果出现问题,请抛出异常。)

正如Joel Abraham所指出的,Java中的数组长度是不变的,我认为我们不知道在阅读之前我们必须处理和返回的最终长度(即考试次数)。因此,这是创建临时ArrayList(可以调整其长度)并在输入包含(while (s.hasNext()))时添加尽可能多的Exams的最简单方法。 要获得类型Exam[]的所需返回值,我们必须将此ArrayList转换回由anyNumberofExams.toArray(new Exam[0])完成的数组。 Java的一些特性是我们必须提供类型Exam[]的数组,在这种情况下长度为0到toArray函数,因此它知道生成的数组必须是哪种类型。

这是一些有效的代码,祝你考试顺利!

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;


public class Exam {

    public static void main(String[] args) {
        Exam[] exams = Exam.readAllExams(new Scanner(
                "Firstname Lastname 1 ExamType 2\n"
                + "Chuck Norris 42 Roundhousekicking 100\n"
                + "Sponge Bob 43 Burgermaking 50"));
        System.out.println(Arrays.toString(exams));
    }

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

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

    public String toString() {
        return String.format("Exam: (firstName: %s, lastName: %s, ID: %d, " 
                +"examType: %s, score: %d)", 
                firstName, lastName, ID, examType, score);
    }

    public static Exam[] readAllExams(Scanner s)
    {
        ArrayList<Exam> anyNumberofExams = new ArrayList<>();
        while (s.hasNext()) {
            anyNumberofExams.add(new Exam(s.next(), s.next(), s.nextInt(), 
                    s.next(), s.nextInt()));
        }
        return anyNumberofExams.toArray(new Exam[0]);
    }

}

编辑:以下是没有ArrayList的示例,但我不建议使用它,因为它对许多考试来说效率低下,对于习惯使用ArrayLists的人来说可读性较差。

public static Exam[] readAllExams(Scanner s)
{
    Exam[] examsArray = new Exam[0];
    int count = 0;
    while (s.hasNext()) {
        Exam exam = new Exam(s.next(), s.next(), s.nextInt(), 
                s.next(), s.nextInt());
        Exam[] newArray = new Exam[count + 1];

        // copy old array to new array - this is very inefficient
        System.arraycopy(examsArray, 0, newArray, 0, examsArray.length);

        examsArray = newArray;
        examsArray[count] = exam;
        count++;
    }
    return examsArray;
}

答案 1 :(得分:0)

如果您只关心数组的构造和操作,那么您可以忽略上述大部分代码。

数组是组织成列表的项目的简单集合。数组可以包含任何数量和类型的项目,也可以是多维的(其中有一个数组数组,还有更多......)。虽然数组是最终的并且无法更改,但ArrayList是一个可变数组(可以更改的数组)。

这个特定的数组通过代码实例化:       Exam [] arrayList = null; 数组名称为&#34; arrayList&#34; 。它使用值 null 初始化,表明它是空的,但是,稍后在代码中使用 arrayList = {firstName,lastName,ID,examValue,score}填充; < / em>的。这将变量 firstName 指定为数组的第0位, lastName 位于第1位,依此类推。最后一个语句返回arrayList; 然后返回数组。