0

ねえ、問題があります。arrayListを使用してstudentStoreを作成したいのですが(これを実行しました)、RandomAccessFileを使用して学生を書き込もうとするとわかりません。

StudentStore details = new StudentStore();
Student a = new Student("Becky O'Brien", "DKIT26", "0876126944", "bexo@hotmail.com");
Student b = new Student("Fabio Borini", "DKIT28", "0876136944", "fabioborini@gmail.com");
Student c = new Student("Gaston Ramirez", "DKIT29", "0419834501", "gramirez@webmail.com");
Student d = new Student("Luis Suarez", "DKIT7", "0868989878", "luissuarez@yahoo.com");
Student e = new Student("Andy Carroll", "DKIT9", "0853456788", "carroll123@hotmail.com");

これらは私の学生です

そして今、私はそれらを書く方法が必要です

RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");
Student er = new Student(null, null, null, null);
for (int i = 0; i < details.length; i++) //This is getting an error obviously because details wont be taken.  nor will a, b,c , d or e which is what the student was declared at the top.
{
    er.setFirstName([i]);
    er.setLastName(lnames[i]);
    er.setAddress(addresses[i]);
    er.setAge(ages[i]);
    er.setSalary(salaries[i]);
    er.write(raf);
}
raf = new RandomAccessFile("employee.dat", "rw");

er = new Student();

int numRecords = (int) raf.length() / er.size();

for (int i = 0; i < numRecords; i++) {
    er.read(raf);

    System.out.print(er.getStudentName() + " ");
    System.out.print(er.getStudentId() + " ");
    System.out.print(er.getStudentEmail() + " ");
    System.out.print(er.getStudentTelephoneNumber() + " ");
}
raf.seek(0);
for (int i = 0; i < numRecords; i++) {
    er.read(raf);
    if (er.getAge() >= 55) {
        er.setSalary(0.0);
        raf.seek(raf.getFilePointer() - er.size());
        er.write(raf);
        raf.seek(raf.getFilePointer() - er.size());
        er.read(raf);
    }
    System.out.print(er.getStudentName() + " ");
    System.out.print(er.getStudentId() + " ");
    System.out.print(er.getStudentEmail() + " ");
    System.out.print(er.getStudentTelephoneNumber() + " ");
}
4

1 に答える 1

2

StudentStore内側のサイズを取得するには、これを試してくださいArrayList

public class StudentStore
{
    ArrayList<Student> students = null;

    public StudentStore()
    {
        students = new ArrayList<Student>();
    }

    public int size()
    {
        return (students == null) ? 0 : students.size();
    }
}

ArrayListただし、単純なものを使用する代わりに、独自のラップを作成する必要がある理由がわかりませんArrayList...

于 2012-08-05T11:39:19.500 に答える