Java中的动态数组列表

时间:2012-12-21 09:33:49

标签: java

我有一些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();

我不了解数组,所以我希望你能把它展示给我。

非常感谢你!

6 个答案:

答案 0 :(得分:3)

public void wsv(ArrayList<Student> list) 

这是一个接受arrayList作为参数的方法。 arrayList是Student对象的集合。所以它可以用...

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

它也没有返回值(它是无效的),请考虑以下方法。

public ArrayList<Student> rsv()

这确实需要返回一个值(类型为Student的ArrayList),但是在没有方法参数的情况下调用它。返回从Object转换为带有...

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

考虑快速阅读casting

答案 1 :(得分:1)

我假设,Student类实现了Serializable接口。

此代码是序列化/反序列化的示例。在方法wsv()中,ArrayList个Student实例被序列化并使用FileOutputStream写入文件。在另一个方法rsv()中读取相同的文件,读取相同的文件(如果文件存在),使用ObjectInputStream对内容进行反序列化,结果将转换为ArrayList并返回。

请参阅Serializable的Javadoc,了解对象的序列化和反序列化。

答案 2 :(得分:1)

第一种方法是::

public void wsv(ArrayList list)

从我的观点来看,wsv意味着写。

它将Student的arrayList写入文件

和::

public ArrayList rsv()

表示阅读

它从文件读取Student的arrayList,如果没有找到则返回新的

这是对象序列化和反序列化的示例

答案 3 :(得分:1)

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

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

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

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

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

答案 4 :(得分:1)

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

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;

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

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

我希望这有所帮助。

答案 5 :(得分:1)

所有剩下的答案几乎澄清了这个问题。我只是想根据你的陈述添加这个:

  

我不了解数组,所以我希望你能把它展示给我。

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

现在您可以填写int数据。

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

但阵列的长度是固定的。

所以我们改用集合。在您的情况下ArrayList

ArrayList<Student>表示它是ObjectStudent的集合。