gpt4 book ai didi

java - 在 Main 中引用 ArrayList

转载 作者:行者123 更新时间:2023-12-01 10:55:54 24 4
gpt4 key购买 nike

我在执行引用 Main 中的 ArrayList 的 System.out.print() 时遇到问题。我的代码...

import java.util.*;

public class Roster {

public static void main(String[]args){

ArrayList<Student> StudentArray = new ArrayList<Student>();

StudentArray.add(new Student("1","John","Smith","John1989@gmail.com", 20, 88, 79, 59));
StudentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmailcom", 19, 91, 72, 85));
StudentArray.add(new Student("3","Jack","Napoli","The_lawyer99yahoo.com", 19, 85, 84, 87));
StudentArray.add(new Student("4","Erin","Black","Erin.black@comcast.net", 22, 91, 98, 82));
StudentArray.add(new Student("5","Jack","Black","jblack14@wgu.edu", 65, 99, 98, 97));
//Example of printing specific student data using getters.
System.out.println("");
for (Student a: StudentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());}
}
}

public static void print_all(){
System.out.println("");
for (Student s: StudentArray) {
System.out.printf("%s\n",s);
}
}
//Print All Student Info
}

学生类(class)

public class Student {
private String StuID;
private String FName;
private String LName;
private String Email;
private int Age;
private double Grade1;
private double Grade2;
private double Grade3;

public Student (String stuid, String fname, String lname, String email,
int age, double grade1, double grade2, double grade3)
{
this.StuID = stuid;
this.FName =fname;
this.LName = lname;
this.Email = email;
this.Age = age;
this.Grade1 = grade1;
this.Grade2 = grade2;
this.Grade3 = grade3;
}
public String getStuID(){
return this.StuID;
}

public String getFName(){
return this.FName;
}

public String getLName(){
return this.LName;
}

public String getEmail(){
return this.Email;
}

public int getAge(){
return this.Age;
}

public double getGrade1(){
return this.Grade1;
}

public double getGrade2(){
return this.Grade2;
}

public double getGrade3(){
return this.Grade3;
}

public String setStuID(String newStuID){
return (this.StuID= newStuID);
}

public String setFName(String newFName){
return (this.FName= newFName);
}

public String setLName(String newLName){
return (this.LName= newLName);
}

public String setEmail(String newEmail){
return (this.Email= newEmail);
}

public int setAge(int newAge){
return (this.Age= newAge);
}

public double setGrade1(double newGrade1){
return (this.Grade1= newGrade1);
}

public double setGrade2(double newGrade2){
return (this.Grade2= newGrade2);
}

public double setGrade3(double newGrade3){
return (this.Grade1= newGrade3);
}


public String toString() {
return String.format("StuID: %s\t First Name: %s\t Last Name: %s\t E-Mail: %s\t Age: %s\t Grade1: %s\t Grade2: %s\t Grade3: %s\t", this.StuID, this.FName, this.LName, this.Email,
this.Age, this.Grade1, this.Grade2, this.Grade3);
}
}

我知道这对于某些(或大多数)人来说可能是一项简单的任务,但过去几天我一直在努力解决这个问题。如果我将“print_all”移动到 Main (如“Example”)方法中,它就可以正常工作。但该练习需要一个引用 Main 的新方法。如果您能提供帮助,我将不胜感激。我大学的 Material 对这一点的解释很糟糕。谢谢。

最佳答案

也许你想做这样的事情:

import java.util.ArrayList;
public class Roster {
ArrayList<Student> studentArray;

public Roster(ArrayList<Student> ar)
{
studentArray=ar;
}

public void print_all(){
System.out.println("");{
for (Student s: studentArray) {
System.out.printf("%s\n",s);}}
//Print All Student Info
}

public static void main(String[]args){

ArrayList<Student> studentArray = new ArrayList<Student>();

studentArray.add(new Student("1","John","Smith","John1989@gmail.com", 20, 88, 79, 59));
studentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmailcom", 19, 91, 72, 85));
studentArray.add(new Student("3","Jack","Napoli","The_lawyer99yahoo.com", 19, 85, 84, 87));
studentArray.add(new Student("4","Erin","Black","Erin.black@comcast.net", 22, 91, 98, 82));
studentArray.add(new Student("5","Jack","Black","jblack14@wgu.edu", 65, 99, 98, 97));
//Example of printing specific student data using getters.

Roster r=new Roster(studentArray);
r.print_all();
}

}

正如您所看到的,方法 print_all 现在是类 Roster 的一个方法,您可以在构造函数中传递在 main 中初始化的数组的引用。

关于java - 在 Main 中引用 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611114/

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