Java 如何创建和返回对象数组?
我对对象/类概念非常陌生,并且在应用它们时已经遇到了困难。请仅解释/显示下面的方法如何创建和返回检查对象数组?我需要从传递给方法的文本文件中获取数据。提前谢谢!我真的很感激Java 如何创建和返回对象数组?,java,arrays,Java,Arrays,我对对象/类概念非常陌生,并且在应用它们时已经遇到了困难。请仅解释/显示下面的方法如何创建和返回检查对象数组?我需要从传递给方法的文本文件中获取数据。提前谢谢!我真的很感激 public Exam(String firstName, String lastName, int ID, String examType, int score) { this.firstName = firstName; this.lastName = lastName; this.ID = ID
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;
}
如果您只关心数组的构造和操作,那么可以忽略上面大部分代码 数组是简单地组织到列表中的项的集合。数组可以包含任意数量和类型的项,也可以是多维的(其中有数组数组,等等…)。虽然数组是最终的且不能更改,但ArrayList是一个可变数组(可以更改的数组) 此特定数组通过以下代码实例化: 检查[]arrayList=null;
数组的名称为“arrayList”。它是用null值初始化的,表示它是空的,但是稍后在代码中用arrayList={firstName,lastName,ID,examValue,score};填充它;。这将变量firstName指定在数组的位置0,lastName指定在位置1,依此类推。最终语句返回arrayList;然后返回数组。我假设您的输入是每行一次检查,每行的格式如下:
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())
)。
要获得类型Exam[]
的所需返回值,我们必须将此数组列表转换回一个数组,该数组由anynumberofexames.toArray(新的检查[0])
完成。Java的某些特性是,我们必须为test[]
函数提供一个类型为toArray
的数组,在本例中,长度为0,以便它知道生成的数组必须是哪种类型
这是一些工作代码,祝你考试顺利
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]);
}
}
你在哪?你在哪?(谁的主意是在注释中添加一个过滤器,阻止人们询问您尝试了什么…)使用实际的
List arrayList=new arrayList()代替数组
并调用arrayList.add(新考试(名字、姓氏、ID、examvalue、分数))每次迭代时,
都使用code>。@chancea,谢谢,但我不允许使用ArrayList谢谢!没有arrayList可以吗@dauer@jordan是的,在没有ArrayList的情况下是可能的,但是在阅读和计算考试之前,你可能知道考试的数量,或者在我看来,考试变得很糟糕。(这将导致您自己编写某种基本的ArrayList,但Java编写良好且经过良好测试的ArrayList在这方面做得非常好。)
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;
}