黑莓手机:如何使(例如哈希表)可持久化的数据结构? [英] Blackberry: how to make a data structure (for example Hashtable) persistable?

查看:295
本文介绍了黑莓手机:如何使(例如哈希表)可持久化的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code效果很好:

I have this code which works well:

static {
    myStore = PersistentStore.getPersistentObject(MY_LONG_KEY);
    myList = (Vector) myStore.getContents();
    if (_storedStations == null) {
            myList = new Vector(4);
            // prepopulate with fake data
            myList.addElement(new MyItem("Eins"));
            myList.addElement(new MyItem("Zwei"));
            myList.addElement(new MyItem("Drei"));
            myList.addElement(new MyItem("Vier"));
            myStore.setContents(myList);
    }
}

class MyItem implements Persistable {
    .....
}

大概是因为<一href=\"http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/PersistentObject.html\"相对=nofollow>的持久性对象DOC 说:

隐持久性

请注意,某些类隐含持久化:布尔,字节,字,整数,长,对象,短,字符串,向量,哈希表

还要注意的是,当你坚持下去的对象,任何持久化对象,它是指也将得到保留。

但对于其他的数据结构?例如 - 而不是上面所用的载体 - 我想有一个的的Hashtable 连键:字符串和值:MyItems。我怎样才能使它持久化,并确保MyItem内容实在存储,无论该对象有多么复杂?

But what about other data structures? For example - instead of the Vector used above - I'd like to have a Hashtable with keys: Strings and values: MyItems. How can I make it Persistable and ensure that MyItem contents is really stored, no matter how complex that object is?

和为什么 IntHashtable 列为持久化的,但是哈希表是不是?

And why is IntHashtable listed as Persistable, but Hashtable is not?

感谢您!
亚历克斯

Thank you! Alex

推荐答案

您自己的哈希表中列出,当你写的隐式持久性的部分,不知道更要验证它是持久化的东西。无论如何,如果你想MyItems是持久化的,只是做公共类MyItems实现持久化作为它的声明。它的工作方式是,如果你想要的东西是持久化的,任何它保持的(也就是说,它的成员变量)的副本需要可持久的为好。所以,如果MyItems需要存储一个字符串,整数向量和MyItem,你需要确保MyItem是持久化的为好。

You listed Hashtable yourself when you wrote the part about Implicit Persistence, not sure what more you want to verify that it is Persistable. At any rate, if you want MyItems to be Persistable, just do public class MyItems implements Persistable as its declaration. The way it works is that if you want something to be Persistable, anything that it keeps a copy of (ie, it's member variables) needs to be Persistable as well. So if MyItems needs to store a String, a Vector of Integers, and a MyItem, you need to make sure MyItem is Persistable as well.

这会给你的依据是你救什么一定的局限性。例如,您将不能创建一个持久化MyItems如果它的成员变量之一是位图。还要注意的是使用内部的方法将从能够使课堂不是prevent你本身持久化的。

This will give you some limitations on what you can save. For instance, you won't be able to create a Persistable MyItems if one of its member variables is a Bitmap. Also note that using a non-Persistable object inside of a method won't prevent you from being able to make the class itself Persistable.

这篇关于黑莓手机:如何使(例如哈希表)可持久化的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆