1

我有几行java代码

class FileManager
{
    File f = new File("SD.DAT");

    public void wsv(ArrayList<Student> list) throws IOException
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(f);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(list);
            fos.close();
            oos.close();
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(FileManager.class.getName()).log(L evel.SEVERE, null, ex);
        }
    }

    public ArrayList<Student> rsv() throws ClassNotFoundException, IOException
    {
        if (!f.exists())
        {
            return new ArrayList<SinhVien>();
        }

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
        return (ArrayList<Student>) ois.readObject();
    }
}

我想问:在下面的代码中,什么是:

public void wsv(ArrayList<Student> list) 

public ArrayList<Student> rsv()

意思是?

为什么它必须返回(ArrayList<Student>) ois.readObject();

我不了解数组,所以我希望你能告诉我。

太感谢了!

4

6 回答 6

3
public void wsv(ArrayList<Student> list) 

This is a method accepting an arrayList as a parameter. The arrayList is a collection of Student objects. So it can be invoked with...

List<Student> myList = new ArrayList<Student>();
wsv(myList);

It also has no return value (it's void), consider the method below.

public ArrayList<Student> rsv()

This does need to return a value (an ArrayList of type Student), but is invoked with no method parameters. The return is cast from an Object to an ArrayList with...

return (ArrayList<Student>) ois.readObject();

Consider having a quick read of casting

于 2012-12-21T09:39:25.720 回答
1

I assume, the Student class implements the Serializable interface.

This code is an example for serialization/deserialization. In the method wsv() an ArrayList of Student instances is serialized and written to a file using a FileOutputStream. In the other method rsv() the same file is read , the same file is read (if the file exists), the content is deserialized using an ObjectInputStream, the result is casted to an ArrayList and returned.

See the Javadoc of Serializable as an introduction to serialization and deserialization of objects.

于 2012-12-21T09:39:55.917 回答
1

第一种方法是::

public void wsv(ArrayList list)

从我的角度来看,这里 wsv 表示 Write 。

它将Student的arrayList写入文件

和::

public ArrayList rsv()

表示阅读

它从文件中读取学生的arrayList,如果没有找到返回新的

这是一个对象序列化和反序列化的例子

于 2012-12-21T09:40:24.740 回答
1

为什么它必须返回(ArrayList<Student>) ois.readObject();
因为方法签名是

 public ArrayList<Student> rsv() throws ClassNotFoundException, IOException 

它期望返回一个 Student 类型的 ArrayList。

(ArrayList<Student>) ois.readObject(); 

读取一个序列化对象并转换为 Student 类型的 ArrayList,然后从该方法返回。

于 2012-12-21T09:40:34.130 回答
1

它们都是方法签名。第一个表示不返回值的方法,由 表示void,并接受并ArrayList包含Student对象作为它的唯一参数:

public void wsv(ArrayList<Student> list) 

具有void返回类型的方法不需要return在方法体中出现语句。

第二个,返回一个ArrayList包含Student对象并且不接受任何参数的:

public ArrayList<Student> rsv()

public是一个访问修饰符,它控制方法的可见性,即可以从哪里调用方法。

如果我们分解代码,我们可以看到您为什么需要(ArrayList<Student>) ois.readObject().

readObject()返回一个Object,这不能从方法中返回,只能ArrayList<Student>接受:

final Object object = ois.readObject();

因此我们需要转换对象,例如让它显示为另一种类型:

final ArrayList<Student> students = (ArrayList<Student>) object;

现在可以从该方法返回:

return students;

在这种情况下,如果我们可以确定读取的对象确实是 type ,我们可以稍微安全地转换Objectto 。否则,强制转换可能是一种危险的情况,并且在尝试强制转换为不可能的类型时可能会抛出 a,例如:ArrayList<Student>ArrayList<Student>ClassCastException

final String string = (String) ois.readObject();

我希望这会有所帮助。

于 2012-12-21T09:52:39.973 回答
1

所有剩余的答案都几乎澄清了这个问题。我只是想根据您的陈述添加此内容:

我不了解数组,所以我希望你能告诉我。

array是相似数据类型的集合。例如: int num = new int[2]; 创建一个长度为 2 且数据类型为 的数组int

int现在您可以用数据填充它。

说:num[0] = 1, num[1] = 5

但是数组有固定的长度。

所以我们使用集合来代替。在你的情况下ArrayList

ArrayList<Student>意味着它是Objects 类型的集合Student

于 2012-12-21T09:52:40.893 回答