简体   繁体   English

Java中的动态数组列表

[英]Dynamic Array List in Java

I have some lines of java code 我有几行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();
    }
}

I want to ask: In the below code, what does: 我想问:在下面的代码中,是什么:

public void wsv(ArrayList<Student> list) 

public ArrayList<Student> rsv()

mean? 意思?

Why it has to return (ArrayList<Student>) ois.readObject(); 为什么必须返回(ArrayList<Student>) ois.readObject();

I don't understand about array, so I hope you can show it to me. 我对数组不了解,所以希望您可以向我展示。

Thank you so much! 非常感谢!

public void wsv(ArrayList<Student> list) 

This is a method accepting an arrayList as a parameter. 这是一种接受arrayList作为参数的方法。 The arrayList is a collection of Student objects. arrayList是Student对象的集合。 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. 这确实需要返回一个值(Student类型的ArrayList),但是没有方法参数被调用。 The return is cast from an Object to an ArrayList with... 返回值从Object强制转换为ArrayList

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

Consider having a quick read of casting 考虑快速阅读铸造

I assume, the Student class implements the Serializable interface. 我假设,Student类实现了Serializable接口。

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. 在wsv()方法中,将学生实例的ArrayList序列化并使用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. 在另一种方法rsv()中,读取相同的文件,读取相同的文件(如果文件存在),使用ObjectInputStream对内容进行反序列化,结果转换为ArrayList并返回。

See the Javadoc of Serializable as an introduction to serialization and deserialization of objects. 有关对象的序列化和反序列化的介绍,请参见序列化的Javadoc。

First method is :: 第一种方法是::

public void wsv(ArrayList list)

From my point of view here wsv means Write . 在我看来,wsv表示Write。

It writes the arrayList of Student in file 它将Student的arrayList写入文件

AND:: 和::

public ArrayList rsv()

means Read 意味着读

It reads the arrayList of Student from file and if not found return the new one 它从文件中读取Student的arrayList,如果找不到,则返回新的

This is an example of Object Serialization and deserialization 这是对象序列化和反序列化的示例

Why it has to return (ArrayList<Student>) ois.readObject(); 为什么必须返回(ArrayList<Student>) ois.readObject();
Because method signature is 因为方法签名是

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

It is expecting a ArrayList of type Student in return. 它期望返回类型为Student的ArrayList。

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

reads a serialized object and casts to ArrayList of type Student which is then returned from the method. 读取序列化的对象,并将其转换为Student类型的ArrayList,然后从该方法返回。

They are both method signatures. 它们都是方法签名。 The first indicates a method which does not return a value, indicated by void , and accepts and ArrayList containing Student objects as it's only argument: 第一个指示不返回值的方法,该方法由void指示,并且接受包含Student对象的ArrayList作为唯一参数:

public void wsv(ArrayList<Student> list) 

A method with a void return type does not require a return statement to be present in the method-body. 具有void返回类型的方法不需要在方法主体中存在return语句。

The second, returns an ArrayList that will contain Student objects and accepts no arguments: 第二个,返回一个ArrayList ,它将包含Student对象并且不接受任何参数:

public ArrayList<Student> rsv()

public is an access modifier, which controls the visibility of the methods, ie where the method can be invoked from. public是访问修饰符,它控制方法的可见性,即可以从何处调用方法。

If we break down the code we can see why you need (ArrayList<Student>) ois.readObject() . 如果我们分解代码,可以看到为什么需要(ArrayList<Student>) ois.readObject()

readObject() returns an Object , this cannot be returned from the method, only ArrayList<Student> is acceptable: readObject()返回一个Object ,它不能从方法中返回,只有ArrayList<Student>是可接受的:

final Object object = ois.readObject();

Therefore we need to cast the object, eg make it appear as another type: 因此,我们需要转换对象,例如使它显示为另一种类型:

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

This can now be returned from the method: 现在可以从方法中返回:

return students;

We can cast the Object to ArrayList<Student> , somewhat safely, in this situation if we can be sure that the object read is really of the type ArrayList<Student> . 我们可以投的ObjectArrayList<Student> ,有些安全,在这种情况下,如果我们可以肯定的是读取对象是真正的类型ArrayList<Student> Otherwise casting can be a dangerous situation and may throw a ClassCastException when trying to cast to a type that isn't possible, for example: 否则,转换可能是一种危险的情况,并且在尝试转换为不可能的类型时可能会引发ClassCastException ,例如:

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

I hope that helps somewhat. 我希望这会有所帮助。

All the remaining answers pretty much clarify the matter. 剩下的所有答案几乎都可以说明问题。 I just thought to add this in light of your statement : 我只是想根据您的声明添加以下内容:

I don't understand about array, so I hope you can show it to me. 我对数组不了解,所以希望您可以向我展示。

array is a collection of similar data types. array是相似数据类型的集合。 For example: int num = new int[2]; 例如: int num = new int[2]; creates an array of length 2 with data type int . 创建一个长度为2的数组,其数据类型为int

Now you can fill it up with int data. 现在您可以用int数据填充它。

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

But arrays have fixed length. 但是数组的长度是固定的。

So we use collections instead. 因此,我们改为使用集合。 In your case ArrayList . 在你的情况下ArrayList

ArrayList<Student> implies that it is a collection of Object s of type Student . ArrayList<Student>表示它是Student类型的Object的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM