gpt4 book ai didi

java - 如何访问不同类中的ArrayList

转载 作者:行者123 更新时间:2023-11-30 07:46:12 25 4
gpt4 key购买 nike

我有一个 Main.java 文件,在这个类中我创建了一个学生对象的 ArrayList。我想将这些对象传递给另一个名为 IntroSentenceHigh 的包中的另一个类。

这是我的 Main.java 文件中的代码:

public class Main 
{

public static void main(String[] args)
{
ArrayList<Student> StudentList = new ArrayList<Student>();
// Student details --- Name, Year Level, Gender, Average Test Score, Exam Score, Classwork, Homework, Practical Work, Behaviour
Student student1 = new Student("Bob", 12, "M", 100, 100, 1, 1, 1, 1);
Student student1 = new Student("John", 12, "M", 100, 100, 1, 1, 1, 1);

StudentList.add(student1);
StudentList.add(student2);

// Loop through all student data in the array
for (int i = 0; i < StudentList.size(); i++)
{
System.out.println(StudentList.get(i).viewStudentInfo());
}

IntroSentenceHigh introSentenceHigh = new IntroSentenceHigh();

我有一个 Student.java 类,其中包含以下内容:

public class Student 
{
private String name;
private int yearLevel;
private String gender;

// Student marks
private int testAverage;
private int exam;

// Other student data
private int classwork;
private int homework;
private int practicalWork;
private int behaviour;

public Student(String name, int yearLevel, String gender, int testAverage, int exam, int classwork,
int homework, int practicalWork, int behaviour)
{
this.name = name;
this.yearLevel = yearLevel;
this.gender = gender;
this.testAverage = testAverage;
this.exam = exam;

// Student's are given scores from 1 to 3.
// 1 is best, 3 is worst
this.classwork = classwork;
this.homework = homework;
this.practicalWork = practicalWork;
this.behaviour = behaviour;
}

public String getName()
{
return name;
}

public int getYearLevel()
{
return yearLevel;
}

public String getGender()
{
return gender;
}

public int getTestAverage()
{
return testAverage;
}

public int getExam()
{
return exam;
}

public int getClasswork()
{
return classwork;
}

public int getHomework()
{
return homework;
}

public int getPracticalWork()
{
return practicalWork;
}

public int getBehaviour()
{
return behaviour;
}

public String viewStudentInfo()
{
String studentInfo;
studentInfo = "Student name: " + getName() + "\n" +
"Year: " + getYearLevel() + "\n" +
"Gender: " + getGender() + "\n" +
"Test Average: " + getTestAverage() + "\n" +
"Exam result: " + getExam()+
"\n";
return studentInfo;
}
}

最后,我有一个 IntroSentenceHigh.java 类,其中包含以下代码:

public class IntroSentenceHigh 
{

private String introSentenceHigh1 = "Student statement 1";
private String introSentenceHigh2 = "Student statement 2";

public String getIntroSentenceHigh1()
{
return introSentenceHigh1;
}

public String getIntroSentenceHigh2()
{
return introSentenceHigh2;
}

基本上,我想在 IntroSentenceHigh 类中访问学生姓名和性别,但我不知道如何从创建 ArrayList 的 Main.java 类中获取它。我寻找同一主题的答案,但由于我是初学者,我不知道如何修改我的代码以使其工作。

任何帮助都会很棒。谢谢!

最佳答案

有很多方法可以完全满足您的要求,最简单的可能是使用参数化构造函数。这意味着向 IntroSentenceHigh 类添加一个 ArrayList 参数(顺便说一下,这不是一个非常直观的名称),并在该类中设置一个等于 ArrayList 的全局变量。这是一种方法:

public Class IntroSentenceHigh { 

private ArrayList<Student> studentList;

public IntroSentenceHigh(ArrayList<Student> studentList) {
this.studentList = studentList
}

现在,您的全局变量 StudentList 包含您在 Main 中创建的 Students ArrayList。然后,您可以使用在 Student 类中声明的 getter 来接收所需的信息。一个例子是:

String aName = studentList.get(0).getName();

记住在创建 IntroSentenceHigh 实例时添加 ArrayList 参数(幸运的是,编译器也会提醒您该要求​​;))

关于java - 如何访问不同类中的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33920938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com