0

Can anyone please explain this code?. like how the objects are being manipulated here? What is being returned and where does it go? and when making the object what does (al) do in

al = fa.fetchArrayList(al);

btw this class is used to deserialize an ArrayList from a file which I serialized earlier

public class FetchArrayList {
    ArrayList<Word> fetchArrayList(ArrayList<Word> arrayList)
    {
        ArrayList<Word>  al = new ArrayList<Word>();
        FileInputStream fis = new FileInputStream("C:/xyz");
        ObjectInputStream ois = new ObjectInputStream(fis);

        al.addAll((ArrayList<Word>) ois.readObject());
        ois.close();
        return al;
        //this part i know
    }
}

Here is how a make an object of this class

ArrayList al = new ArrayList();
FetchArrayList fa = new FetchArrayList();
al = fa.fetchArrayList(al);

How can i make this code better? im a rookie here lol

Stefan
  • 1,433
  • 1
  • 19
  • 30
xtabbas
  • 627
  • 4
  • 10
  • 18
  • 3
    Can you please add what are you trying to achieve here, as this method is not really clear? What to you need the arrayList parameter for? – Norbert Radyk Apr 16 '14 at 11:41
  • As far as I can tell, passing `al` as parameter does excatly nothing in your code. Did you mean to fill in a pre-existing ArrayList? –  Apr 16 '14 at 13:14
  • @Norbert look at this question i asked earlier [http://stackoverflow.com/questions/23044159/how-to-serialize-arraylist-2x-and-dont-over-write-the-already-present-arraylist?noredirect=1#comment35209867_23044159]. might help. Im saving some objects in arraylist and saving that arraylist in file. and from this method deserializing the whole arraylist into another arraylist to use it further. Dont know why i used the parametrized method exactly and thats what i want to know. – xtabbas Apr 16 '14 at 13:15
  • In addition to @Arkadiy's suggestion, why not make `fetchArrayList` a static method so you can call `FetchArrayList.fetchArrayList()` (and improve the class/method names as it is confusing right now)? – Stefan Apr 16 '14 at 13:17
  • I suggest you take out things which don't do anything. e.g. you pass an argument which isn't used. You copy the list with addAll even though this makes no difference. The method is non-static even though you don't use the instance of FetchArrayList. – Peter Lawrey Apr 16 '14 at 15:33

2 Answers2

0

For the sake of answering the question, I'll put the suggestions in the comments in here so the question can be marked as answered.

Arkadiy's suggestion
Your fetchArrayList required an ArrayList as a parameter, but it doesn't do anything with it. You can remove the parameter from the method's signature.

My suggestion
If I interpret your class correctly, you use it only for reading the ArrayList object from a file. If so, you can declare your fetchArrayList method as a static method (see here for a little more explanation). That way you won't have to create a new instance of the FetchArrayList class, which you probably won't use after you read the ArrayList from a file (you can then call FetchArrayList.fetchArrayList()). Note that due to the class and method names, this call looks weird. I suggest refactoring the class name into something more logical.

Community
  • 1
  • 1
Stefan
  • 1,433
  • 1
  • 19
  • 30
0

This is how I might write the code.

public enum IOTools {;
    public static <T> List<T> readList(String filename) throws IOException {
        try (ObjectInputStream ois = new ObjectInputStream(
                                         new FileInputStream(filename))) {

            return (ArrayList<T>) ois.readObject());
    }
}

Then you can write

List<Words> words = IOTools.readList("words.list");
List<String> strings = IOTools.readList("strings.list");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130