Alfred Seattle
Alfred Seattle

Reputation: 255

Dynamic Array List in Java

I have some lines of java code

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();

I don't understand about array, so I hope you can show it to me.

Thank you so much!

Upvotes: 1

Views: 564

Answers (6)

Prateek
Prateek

Reputation: 12242

First method is ::

public void wsv(ArrayList list)

From my point of view here wsv means Write .

It writes the arrayList of Student in file

AND::

public ArrayList rsv()

means Read

It reads the arrayList of Student from file and if not found return the new one

This is an example of Object Serialization and deserialization

Upvotes: 1

Daniel Hiller
Daniel Hiller

Reputation: 3485

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.

Upvotes: 1

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

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. For example: int num = new int[2]; creates an array of length 2 with data type int.

Now you can fill it up with int data.

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

But arrays have fixed length.

So we use collections instead. In your case ArrayList.

ArrayList<Student> implies that it is a collection of Objects of type Student.

Upvotes: 1

Jonathan
Jonathan

Reputation: 20375

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:

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.

The second, returns an ArrayList that will contain Student objects and accepts no arguments:

public ArrayList<Student> rsv()

public is an access modifier, which controls the visibility of the methods, i.e where the method can be invoked from.

If we break down the code we can see why you need (ArrayList<Student>) ois.readObject().

readObject() returns an Object, this cannot be returned from the method, only ArrayList<Student> is acceptable:

final Object object = ois.readObject();

Therefore we need to cast the object, e.g. 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>. 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:

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

I hope that helps somewhat.

Upvotes: 1

Ajinkya
Ajinkya

Reputation: 22710

Why it has to return (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.

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

reads a serialized object and casts to ArrayList of type Student which is then returned from the method.

Upvotes: 1

David
David

Reputation: 20063

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

Upvotes: 3

Related Questions