我在打印一个学生商店时遇到了一些问题,我使用ArrayList
. 然后我制作了一个静态数组来容纳这些不同的学生,现在我正在尝试找出我可以使用什么方法来编写它们。这是我的代码:
MainApp
import java.io.RandomAccessFile;
public class MainApp
{
public static void main(String[] args) throws Exception
{
new MainApp().start();
}
public void start()throws Exception
{
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");
details.add(a);
details.add(b);
details.add(c);
details.add(d);
details.add(e);
//details.print();
RandomAccessFile file = new RandomAccessFile("ContactDetails.txt","rw");
Student[] students = {a,b,c,d,e};
for (int i = 0;i < students.length;i++)
{
file.writeByte(students[i]);
}
file.close();
}
}
该行file.writeByte(students[i]);
不正确,我找不到适合此的方法。错误读取writeByte(int)
类型RandomAccessFile
中的方法不适用于参数(Student
)。这显然是因为writeBytes
方法不带学生类型。