Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图创建一个我可以进一步操作的学生数据库_Java_Arrays_Object_Arraylist - Fatal编程技术网

Java 试图创建一个我可以进一步操作的学生数据库

Java 试图创建一个我可以进一步操作的学生数据库,java,arrays,object,arraylist,Java,Arrays,Object,Arraylist,我正在尝试创建一个数据库(作为ArrayList),用于存储学生信息(对象数组),但是我的“addStudent()”函数出现了问题,因为它似乎只使用通过调用该函数创建的最新数组。我知道我可能错过了眼前的某件事,但我已经开始全力以赴地思考,我相信我会要求换一双新眼睛。非常感谢您的帮助。我还想问,如果你有任何关于我需要实现的代码的指针,以便执行我试图用这个程序实现的操作,请继续指出这些指针。谢谢各位。我的代码是: import java.util.ArrayList; import java.ut

我正在尝试创建一个数据库(作为ArrayList),用于存储学生信息(对象数组),但是我的“addStudent()”函数出现了问题,因为它似乎只使用通过调用该函数创建的最新数组。我知道我可能错过了眼前的某件事,但我已经开始全力以赴地思考,我相信我会要求换一双新眼睛。非常感谢您的帮助。我还想问,如果你有任何关于我需要实现的代码的指针,以便执行我试图用这个程序实现的操作,请继续指出这些指针。谢谢各位。我的代码是:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Record {

// Create an array to hold the Student's info & List to hold those student's
static Object[] student = new Object[10];
static List<Object> database = new ArrayList<Object>();

// Create Student's info variables
private int studentID;
private String lastName;
private String firstName;
private long phoneNumber;
private String major;
private double gpa;
private int birthYear;
private String birthMonth;
private int birthDay;
private String homeAddress;

/* For FUTURE tasks */
public void setStudentID( int idGiven ) {

    studentID = idGiven;
}

public void setLastName( String lastNameGiven ) {

    lastName = lastNameGiven;
}

public void setFirstName( String firstNameGiven ) {

    firstName = firstNameGiven;
}

public void setPhoneNumber( long numGiven ) {

    phoneNumber = numGiven;
}

public void setMajor( String majorGiven ) {

    major = majorGiven;
}

public void setGPA( double gpaGiven ) {

    gpa = gpaGiven;
}

public void setBirthYear( int birthYearGiven ) {

    birthYear = birthYearGiven;
}

public void setBirthMonth( String birthMonthGiven ) {

    birthMonth = birthMonthGiven;
}

public void setBirthDay( int birthDayGiven ) {

    birthDay = birthDayGiven;
}

public void setHomeAddress( String homeAddressGiven ) {

    homeAddress = homeAddressGiven;
}

// Function to create new student when one is entered
static public Object[] newStudent( int newStudentID, String newLastName, String newFirstName, long newPhoneNumber, String newMajor, double newGPA, int newBirthYear, String newBirthMonth, int newBirthDay, String newHomeAddress )
{

    student[0] = newStudentID;
    student[1] = newLastName;
    student[2] = newFirstName;
    student[3] = newPhoneNumber;
    student[4] = newMajor;
    student[5] = newGPA;
    student[6] = newBirthYear;
    student[7] = newBirthMonth;
    student[8] = newBirthDay;
    student[9] = newHomeAddress;

    database.addAll(Arrays.asList(student));



    return student;
}

// Create Function to add the student array to the database list
static public List<Object> addStudent ( Object[] s ) {

    student = s;
    database.add(student);

    return database;
}

// Test Program
public static void main( String [] args ) 
{
    Object[] student1 = Record.newStudent(00000000, "Thomas", "Luke", 9038189888L, "Computer Science", 4.00, 1997, "February", 28, "169 Karen Dr.");
    Object[] student2 = Record.newStudent(00000001, "Doe", "Jane", 5802679999L, "Mathematics", 3.62, 1990, "March", 1, "100 Circle Dr.");
    Object[] student3 = Record.newStudent(00000002, "Baker", "Joe", 9805776452L, "Safety", 2.75, 1998, "December", 7, "200 Circle Dr.");
    //Object[] student4 = Record.newStudent(00000003, "Williams", "Doug", 9407778888L, "Business", 1.95, 2000, "May", 7, "501 West Maple St.");

    Record.addStudent(student1);
    Record.addStudent(student2);
    Record.addStudent(student3);
    //Record.addStudent(student4);

    //Print out Student1's Info from the created student & from its index in the List containing all students
    System.out.println(Arrays.toString(student1));
    System.out.println(database.get(0));

    /* PERFORM TASKS: 
     * 1. delete record(s) from student id 
     * 2. list all student info in the order they were saved
     * 3. Reorder/organize student info ascending & descending by id, gpa, or birthday
     * 4. list students in a given major or given id
     */
}


}
import java.util.ArrayList;
导入java.util.array;
导入java.util.LinkedList;
导入java.util.List;
公开课记录{
//创建一个数组来保存学生的信息&列表来保存这些学生的信息
静态对象[]学生=新对象[10];
静态列表数据库=新的ArrayList();
//创建学生的信息变量
私立国际学生;
私有字符串lastName;
私有字符串名;
专用长电话号码;
私人弦专业;
私人双gpa;
私人生日;
私人串生日月;
私人生日;
私有字符串家庭地址;
/*未来任务*/
public void setStudentID(int-idGiven){
studentID=idGiven;
}
public void setLastName(字符串lastNameGiven){
lastName=lastName给定;
}
public void setFirstName(字符串firstNameGiven){
firstName=firstName给定;
}
公共无效设置电话号码(长数字){
phoneNumber=numGiven;
}
公共void setMajor(字符串majorGiven){
主修=主修;
}
公共无效设置GPA(双gpaGiven){
gpa=gpaGiven;
}
公众出生年份(给出整数个出生年份){
生日=给定的生日;
}
公共void setBirthMonth(字符串birthMonthGiven){
生日月份=生日月份;
}
公众出生日(国际生日){
生日=生日;
}
public void setHomeAddress(给定字符串HomeAddress){
homeAddress=给定的homeAddress;
}
//函数用于在输入新学员时创建新学员
静态公共对象[]newStudent(int newStudentID、String newLastName、String newFirstName、long newPhoneNumber、String newMajor、double newGPA、int newirthyear、String newirthmonth、int newirthday、String newHomeAddress)
{
学生[0]=新学生ID;
学生[1]=新姓氏;
学生[2]=新名字;
学生[3]=新电话号码;
学生[4]=新专业;
学生[5]=新GPA;
学生[6]=新生年;
学生[7]=新生月;
学生[8]=新生日;
学生[9]=新住址;
database.addAll(Arrays.asList(student));
留学生;
}
//Create函数将学生数组添加到数据库列表中
静态公共列表addStudent(对象[]s){
学生=s;
添加(学生);
返回数据库;
}
//测试程序
公共静态void main(字符串[]args)
{
Object[]student1=Record.newStudent(00000000,“托马斯”,“卢克”,9038189888L,“计算机科学”,1997年4月,“2月”,28日,“169卡伦博士”);
Object[]student2=Record.newStudent(00000001,“Doe”,“Jane”,5802679999L,“数学”,3.621990,“March”,1,“100圈博士”);
Object[]student3=Record.newStudent(00000002,“贝克”,“乔”,9805776452L,“安全”,2.751998,“12月”,7日,“200圈博士”);
//Object[]student4=Record.newStudent(00000003,“威廉姆斯”,“道格”,9407778888L,“商业”,1.952000,“五月”,7,“西枫树街501号”);
记录。添加学生(学生1);
记录。添加学生(学生2);
记录。添加学生(学生3);
//记录。添加学生(学生4);
//从创建的学生打印学生1的信息&从包含所有学生的列表中的索引打印
System.out.println(Arrays.toString(student1));
System.out.println(database.get(0));
/*执行任务:
*1.从学生id中删除记录
*2.按保存顺序列出所有学生信息
*3.按id、gpa或生日重新排列/组织学生信息升序和降序
*4.列出特定专业或特定id的学生
*/
}
}
“如果”您绝对只能使用
数组来存储学生信息,这在我看来是错误的,那么在
newStudent
中,您每次都必须创建一个新数组

static public Object[] newStudent( int newStudentID, String newLastName, String newFirstName, long newPhoneNumber, String newMajor, double newGPA, int newBirthYear, String newBirthMonth, int newBirthDay, String newHomeAddress )
{

    Object[] student = new Object[10];
    student[0] = newStudentID;
    student[1] = newLastName;
    student[2] = newFirstName;
    student[3] = newPhoneNumber;
    student[4] = newMajor;
    student[5] = newGPA;
    student[6] = newBirthYear;
    student[7] = newBirthMonth;
    student[8] = newBirthDay;
    student[9] = newHomeAddress;

    database.add(student);

    return student;
}
然后应将整个数组(按原样)添加到
ArrayList

然而,这对我来说似乎是违反直觉的。相反,你应该从“学生”的概念开始,例如

public class Student {

    private int studentID;
    private String lastName;
    private String firstName;
    private long phoneNumber;
    private String major;
    private double gpa;
    private int birthYear;
    private String birthMonth;
    private int birthDay;
    private String homeAddress;

    public Student(int studentID, String lastName, String firstName, long phoneNumber, String major, double gpa, int birthYear, String birthMonth, int birthDay, String homeAddress) {
        this.studentID = studentID;
        this.lastName = lastName;
        this.firstName = firstName;
        this.phoneNumber = phoneNumber;
        this.major = major;
        this.gpa = gpa;
        this.birthYear = birthYear;
        this.birthMonth = birthMonth;
        this.birthDay = birthDay;
        this.homeAddress = homeAddress;
    }

    public int getStudentID() {
        return studentID;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public long getPhoneNumber() {
        return phoneNumber;
    }

    public String getMajor() {
        return major;
    }

    public double getGpa() {
        return gpa;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public String getBirthMonth() {
        return birthMonth;
    }

    public int getBirthDay() {
        return birthDay;
    }

    public String getHomeAddress() {
        return homeAddress;
    }

    @Override
    public String toString() {
        return "Student{" + "studentID=" + studentID + ", lastName=" + lastName + ", firstName=" + firstName + ", phoneNumber=" + phoneNumber + ", major=" + major + ", gpa=" + gpa + ", birthYear=" + birthYear + ", birthMonth=" + birthMonth + ", birthDay=" + birthDay + ", homeAddress=" + homeAddress + '}';
    }

}
然后可以通过某种“数据库”进行管理

它会打印出像

Database database = new Database();

database.add(new Student(00000000, "Thomas", "Luke", 9038189888L, "Computer Science", 4.00, 1997, "February", 28, "169 Karen Dr."));
database.add(new Student(00000001, "Doe", "Jane", 5802679999L, "Mathematics", 3.62, 1990, "March", 1, "100 Circle Dr."));
database.add(new Student(00000002, "Baker", "Joe", 9805776452L, "Safety", 2.75, 1998, "December", 7, "200 Circle Dr."));

//Print out Student1's Info from the created student & from its index in the List containing all students
for (int index = 0; index < database.size(); index++) {
    System.out.println(database.get(index));
}
Student{studentID=0, lastName=Thomas, firstName=Luke, phoneNumber=9038189888, major=Computer Science, gpa=4.0, birthYear=1997, birthMonth=February, birthDay=28, homeAddress=169 Karen Dr.}
Student{studentID=1, lastName=Doe, firstName=Jane, phoneNumber=5802679999, major=Mathematics, gpa=3.62, birthYear=1990, birthMonth=March, birthDay=1, homeAddress=100 Circle Dr.}
Student{studentID=2, lastName=Baker, firstName=Joe, phoneNumber=9805776452, major=Safety, gpa=2.75, birthYear=1998, birthMonth=December, birthDay=7, homeAddress=200 Circle Dr.}

你没抓住重点。不需要
Object[]student
,您的数据库应该受到约束,因此它只能保存
student
列表数据库的实例。然后你的
newStudent
应该返回
Student
的一个实例,然后你
addStudent
只需要你传递
Student
的一个实例,如此类推“如果”你绝对只需要使用
数组来存储学生信息,这在我看来是错误的,那么在
newStudent
中,每次都必须创建一个新数组
Student{studentID=0, lastName=Thomas, firstName=Luke, phoneNumber=9038189888, major=Computer Science, gpa=4.0, birthYear=1997, birthMonth=February, birthDay=28, homeAddress=169 Karen Dr.}
Student{studentID=1, lastName=Doe, firstName=Jane, phoneNumber=5802679999, major=Mathematics, gpa=3.62, birthYear=1990, birthMonth=March, birthDay=1, homeAddress=100 Circle Dr.}
Student{studentID=2, lastName=Baker, firstName=Joe, phoneNumber=9805776452, major=Safety, gpa=2.75, birthYear=1998, birthMonth=December, birthDay=7, homeAddress=200 Circle Dr.}