Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.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 - Fatal编程技术网

Java中的动态数组列表

Java中的动态数组列表,java,Java,我有几行java代码 class FileManager { File f = new File("SD.DAT"); public void wsv(ArrayList<Student> list) throws IOException { try { FileOutputStream fos = new FileOutputStream(f); ObjectOutputStr

我有几行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 ois.readObject

我不懂数组,希望你能给我看看

非常感谢你

public void wsv(ArrayList<Student> list) 
这是一个接受arrayList作为参数的方法。arrayList是学生对象的集合。所以它可以用

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

它也没有返回值,它是空的,考虑下面的方法。< /P>

public ArrayList<Student> rsv()
这确实需要返回一个类型为Student的ArrayList值,但在调用时没有方法参数。将返回从对象强制转换到ArrayList,并使用

return (ArrayList<Student>) ois.readObject();
考虑一下快速阅读一下

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

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

它也没有返回值,它是空的,考虑下面的方法。< /P>

public ArrayList<Student> rsv()
这确实需要返回一个类型为Student的ArrayList值,但在调用时没有方法参数。将返回从对象强制转换到ArrayList,并使用

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

考虑快速阅读一下

我想,Student类实现了接口

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


请参阅的Javadoc,作为对对象序列化和反序列化的介绍。

我假设,Student类实现了接口

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

请参阅的Javadoc作为对象序列化和反序列化的介绍。

第一种方法是:

public void wsv(ArrayList list)
在我看来,wsv在这里的意思是写

它在文件中写入学生的数组列表

及::

意味着阅读

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

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

第一种方法是:

public void wsv(ArrayList list)
在我看来,wsv在这里的意思是写

它在文件中写入学生的数组列表

及::

意味着阅读

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

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

为什么它必须返回ArrayList ois.readObject; 因为方法签名是

 public ArrayList<Student> rsv() throws ClassNotFoundException, IOException 
它需要一个Student类型的ArrayList作为回报

(ArrayList<Student>) ois.readObject(); 
读取序列化对象并强制转换到Student类型的ArrayList,然后从方法返回该ArrayList

为什么必须返回ArrayList ois.readObject; 因为方法签名是

 public ArrayList<Student> rsv() throws ClassNotFoundException, IOException 
它需要一个Student类型的ArrayList作为回报

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

读取序列化对象并强制转换到Student类型的ArrayList,然后从方法返回该ArrayList

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

public void wsv(ArrayList<Student> list) 
public是一个访问修饰符,它控制方法的可见性,即从何处调用方法

如果我们仔细分析代码,就可以了解为什么需要ArrayList ois.readObject

readObject返回一个对象,无法从方法返回,只能接受ArrayList:

final Object object = ois.readObject();
因此,我们需要强制转换对象,例如,使其显示为另一种类型:

final ArrayList<Student> students = (ArrayList<Student>) object;
在这种情况下,如果能够确保读取的对象确实是ArrayList类型的,我们可以将对象强制转换为ArrayList,这在某种程度上是安全的。否则,强制转换可能是一种危险的情况,在尝试强制转换为不可能的类型时,可能会引发ClassCastException,例如:

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

我希望这能有所帮助。

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

public void wsv(ArrayList<Student> list) 
public是一个访问修饰符,它控制方法的可见性,即从何处调用方法

如果我们仔细分析代码,就可以了解为什么需要ArrayList ois.readObject

readObject返回一个对象,无法从方法返回,只能接受ArrayList:

final Object object = ois.readObject();
因此,我们需要强制转换对象,例如,使其显示为另一种类型:

final ArrayList<Student> students = (ArrayList<Student>) object;
在这种情况下,如果能够确保读取的对象确实是ArrayList类型的,我们可以将对象强制转换为ArrayList,这在某种程度上是安全的 T否则,强制转换可能是一种危险的情况,在尝试强制转换为不可能的类型时,可能会引发ClassCastException,例如:

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

我希望这能有所帮助。

剩下的所有答案基本上澄清了这一问题。根据你的陈述,我想补充一下:

我不懂数组,希望你能给我看看

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

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

假设:num[0]=1,num[1]=5

但是数组的长度是固定的

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


ArrayList意味着它是Student类型对象的集合。

所有剩余的答案基本上澄清了这一问题。根据你的陈述,我想补充一下:

我不懂数组,希望你能给我看看

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

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

假设:num[0]=1,num[1]=5

但是数组的长度是固定的

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

ArrayList意味着它是Student类型的对象的集合