【问题标题】:How can I store an arraylist of custom objects?如何存储自定义对象的数组列表?
【发布时间】:2010-12-09 00:55:42
【问题描述】:

我创建了一个由自定义对象组成的数组列表。基本上,用户将创建一个类,并且每次创建一个类时,都会将一个新的 Lecture(我的自定义对象)添加到数组列表中。我需要保存生成的数组列表,这样即使重新启动应用程序也会保存用户的类。

据我了解,我必须使我的课程可序列化。但我究竟该怎么做呢?然后,一旦它被序列化了,我该怎么办?

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

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    我在我正在开发的天气应用程序中使用一个类...

    public class RegionList extends ArrayList<Region> {} // Region implements Serializeable
    

    为了节省我使用这样的代码...

    FileOutputStream outStream = new FileOutputStream(Weather.WeatherDir + "/RegionList.dat");
    ObjectOutputStream objectOutStream = new ObjectOutputStream(outStream);
    objectOutStream.writeInt(uk_weather_regions.size()); // Save size first
    for(Region r:uk_weather_regions)
        objectOutStream.writeObject(r);
    objectOutStream.close();
    

    注意:在编写 Region 对象之前,我会编写一个 int 来保存列表的“大小”。

    当我回读时,我会这样做......

    FileInputStream inStream = new FileInputStream(f);
    ObjectInputStream objectInStream = new ObjectInputStream(inStream);
    int count = objectInStream.readInt(); // Get the number of regions
    RegionList rl = new RegionList();
    for (int c=0; c < count; c++)
        rl.add((Region) objectInStream.readObject());
    objectInStream.close();
    

    【讨论】:

      【解决方案2】:

      你很幸运,你所有的班级成员都已经是可序列化的,所以你的第一步是说 Lecture 是可序列化的。

      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;
          }
      

      接下来,您需要创建一个默认构造函数,因为序列化似乎需要这样做。最后一件事是您需要将对象写入文件。我通常使用类似下面的东西。请注意,这是为了保存游戏状态,因此您可能不想使用缓存目录。

      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 */ }
          }
      }
      

      读回数据与写入相当对称,所以我在这个答案中忽略了这一点。此外,序列化对象仍然有很多警告,所以我建议你做一些谷歌搜索并阅读一般的 Java 序列化。

      【讨论】:

      • 解释减号,我用谷歌搜索找到了你的答案!
      【解决方案3】:

      你可以使用:

      System.Runtime.Serialization
      

      您可以在此处查看示例: Serialization in C#

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多