1

Currently I have a program that can save and load ArrayLists of type "Variable" from a file. However, I would like to be able to save and load different objects(instances of different classes such as "Variables", "Functions"..etc) from just one file. Here is the code I have now:

 public SaveState(){
    ObjectOutputStream out = null;
    try {
        out = new ObjectOutputStream(new FileOutputStream("vars.stt"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ArrayList<Variables> vars = new ArrayList<>();
    vars.add(new Variables("g", 5));
    vars.add(new Variables("f", -3.12));
    try {
        out.writeObject(vars);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public LoadState(){
    ObjectInputStream in = null;
    try {
        in = new ObjectInputStream(new FileInputStream("vars.stt"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        //final Object o = in.readObject();
        final ArrayList<Variables> vars = (ArrayList<Variables>) in.readObject();
        System.out.println(vars.toString());
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
}

This code only allows me to load one object type from a file. Is there any way to save/load multiple object types in this manner?

9
  • didn't you try making a base class for Functions and Variables and using the array list of type Base Class? Commented Apr 13, 2016 at 3:23
  • @ImeshaSudasingha Honestly I'm not sure if I would be able to do that, it would require me to refactor a lot of my code (hundreds of lines). Is there another solution. Commented Apr 13, 2016 at 3:25
  • No, you just can create some base class(say "ProgramElement"), them just extent the Variables and Functions class from it. No need to refactor the code. Commented Apr 13, 2016 at 3:26
  • @ImeshaSudasingha So if I had classes "Variables", "Functions", and "Lists" how would I extend all 3 from one class? Commented Apr 13, 2016 at 3:31
  • Why don't you use Object class as that 'base class'. You can use polymorphism. Commented Apr 13, 2016 at 3:31

2 Answers 2

2

You can simply use a map conataining the arraylists of varaible or functions. Map is good in terms of performance also.

    Map<String, List> map = new HashMap<String, List>();
    map.put("variable", arralist1);
    map.put("functions", arralist2);
    map.put("list", arralist3);
Sign up to request clarification or add additional context in comments.

3 Comments

So this would work even if the ArrayLists are of different types?
Yes. Or you can use generics to make it parameterized.
Thank you very much for this solution. It worked perfectly
2

Create a class hierarchy like this:

//base class. No need to write any content
class ProgramElement {

}

//your variables class
class Variables extends ProgramElement {
    //your code
}

//your functions class
class Functions extends ProgramElement {
    //your code
}

//your lists class
class Lists extends ProgramElement {
    //your code
}

Then use an ArrayList of type ProgramElement. Remember List here is not your Lists class. It's java's List interface.

List<ProgramElement> elements = new ArrayList<>();
elements.add(aFunction); //add function object
elements.add(aList);  //lists object
elements.add(aVariable); //variables object

Note: You can achieve the same by using a List of type Object. But it is not recommended since it is not a good practice.

1 Comment

Thank you very much, at first I thought you were telling me to do something along the lines of "class ProgramElement extends Variables, Functions, Lists { }" That is where I got confused. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.