我怎么能存储自定义对象的ArrayList?自定义、对象、我怎么能、ArrayList

2023-09-05 10:31:41 作者:风沙又吹散离愁

我已经创建了一个由自定义对象的ArrayList。基本上,用户将创建一个类,每次创建一个类时,一个新的演讲(我的自定义对象)添加到ArrayList。我需要保存生成的数组列表所以即使当应用程序重新启动用户的类将被保存。

这是我的理解,我必须让我的课序列化。但我怎么做究竟是什么?然后,一旦其系列化我该怎么办?

 公共类讲座{

公共字符串称号;
公共字符串的startTime;
公共字符串endTime的;
公共字符串天;
公共布尔classEnabled;

公开讲​​座(标题字符串,字符串的startTime,endTime的字符串,字符串日,布尔启用){
    this.title =称号;
    this.startTime = startTime时;
    this.endTime = endTime的;
    this.day =天;
    this.classEnabled =启用;
}
// getter和setter方法​​如下
 

解决方案

你很幸运,你的所有类的成员已经serialzble让你的第一个步骤是说的讲座的是序列化的。

 公共类讲座实现Serializable {

    公共字符串称号;
    公共字符串的startTime;
    公共字符串endTime的;
    公共字符串天;
    公共布尔classEnabled;

    公开讲​​座(标题字符串,字符串的startTime,endTime的字符串,字符串日,布尔启用){
        this.title =称号;
        this.startTime = startTime时;
        this.endTime = endTime的;
        this.day =天;
        this.classEnabled =启用;
    }
 
如何正确使用remove Object o 删除ArrayList中的自定义对象 如何从ArrayList中删除特定的对象

接下来,你需要做一个默认的构造函数,因为序列化似乎要求。的最后一件事是,你需要写你的对象到一个文件。我通常使用类似于下面的东西。请注意,这是用于保存游戏状态,所以你可能不希望使用的缓​​存目录。

 私人无效saveState和(){
    最终文件的cache_dir = this.getCacheDir();
    最终文件suspend_f =新的文件(cache_dir.getAbsoluteFile()+文件分割符+ SUSPEND_FILE);

    FileOutputStream中FOS = NULL;
    ObjectOutputStream的OOS = NULL;
    布尔保持= TRUE;

    尝试 {
        FOS =新的FileOutputStream(suspend_f);
        OOS =新的ObjectOutputStream(FOS);

        oos.writeObject(this.gameState);
    }
    赶上(例外五){
        保持= FALSE;
        Log.e(MyAppName,没有暂停,E);
    }
    最后 {
        尝试 {
            如果(!OOS = NULL)oos.close();
            如果(!FOS = NULL)fos.close();
            如果(保持==假)suspend_f.delete();
        }
        赶上(例外五){/ *做什么* /}
    }
}
 

读回数据是pretty的对称写,所以我离开了这一点对于这个答案。此外,还有很多的注意事项,以序列化对象,所以我建议你做一些谷歌的搜索和一般Java序列化阅读起来。

I have created an arraylist that is made up of custom objects. Basically the user will create a class and every time a class is created, a new Lecture (my custom object) is added to the arraylist. I need to save the generated arraylist so the user's classes will be saved even when the app is restarted.

From my understanding, I have to make my class serializable. But how exactly do I do that? And then once its serialized what do I do?

public class Lecture{

public String title;
public String startTime;
public String endTime;
public String day;
public boolean classEnabled;

public Lecture(String title, String startTime, String endTime, String day, boolean enable){
    this.title = title;
    this.startTime = startTime;
    this.endTime = endTime;
    this.day = day;
    this.classEnabled = enable;
}
//Getters and setters below

解决方案

You're in luck, all of your class' members are already serialzble so your first step is to say that Lecture is Serializable.

public class Lecture implements Serializable {

    public String title;
    public String startTime;
    public String endTime;
    public String day;
    public boolean classEnabled;

    public Lecture(String title, String startTime, String endTime, String day, boolean enable){
        this.title = title;
        this.startTime = startTime;
        this.endTime = endTime;
        this.day = day;
        this.classEnabled = enable;
    }

Next, you need to make a default constructor since serialization seems to require that. The last thing is you need to write your object out to a file. I usually use something like the following. Note this is for saving a game state so you might not want to use the cache directory.

private void saveState() {
    final File cache_dir = this.getCacheDir(); 
    final File suspend_f = new File(cache_dir.getAbsoluteFile() + File.separator + SUSPEND_FILE);

    FileOutputStream   fos  = null;
    ObjectOutputStream oos  = null;
    boolean            keep = true;

    try {
        fos = new FileOutputStream(suspend_f);
        oos = new ObjectOutputStream(fos);

        oos.writeObject(this.gameState);
    }
    catch (Exception e) {
        keep = false;
        Log.e("MyAppName", "failed to suspend", e);
    }
    finally {
        try {
            if (oos != null)   oos.close();
            if (fos != null)   fos.close();
            if (keep == false) suspend_f.delete();
        }
        catch (Exception e) { /* do nothing */ }
    }
}

Reading the data back is pretty symmetric to the write so I have left that out for this answer. Also, there are still a lot of caveats to Serialized objects so I suggest you do some Google searches and read up on Java serialization in general.