简体   繁体   English

如何在Android中将数组保存到内存中

[英]How to save array to memory in Android

i'm new to android and i'ld like to understand how i can save an array to memory. 我是android的新手,我想知道如何将数组保存到内存中。 I don't mean internal memory, as i know about shared preferences, external storage and sqlite. 我不是指内部记忆,因为我知道共享偏好,外部存储和sqlite。 I'm looking at something within the range of RAM/Heap. 我正在寻找RAM /堆范围内的东西。

The actual challenge is that i have a string array of names. 实际的挑战是我有一个字符串数组的名称。 I get the names from an sqlite database when app is first started. 首次启动应用程序时,我从sqlite数据库中获取名称。

eg 例如

String[] names = new String[] {"dave", "james", "tommy" } 

This list can grow to contain over 10000 items. 此列表可以增长到包含超过10000个项目。

In another activity, i want to check if some names exist in the list. 在另一个活动中,我想检查列表中是否存在某些名称。 I don't want to query the database back and forth to achieve this. 我不想来回查询数据库来实现这一点。 That is why i'm thinking that saving in memory may just be the best bet as it can improve performance. 这就是为什么我认为节省内存可能只是最好的选择,因为它可以提高性能。

So i'ld like to know how to save this array in memory and read from it in another activity. 所以我想知道如何将这个数组保存在内存中并在另一个活动中读取它。 I already know how to manage my RAM i'm just looking for ways to get this info there and read from it. 我已经知道如何管理我的RAM我只是想找到方法来获取这些信息并从中读取。

Thanks 谢谢

Create a Singleton Pattern and put your array into it and you can share your array across your app instance but remember this singleton class will be destroyed with the app so will your you array inside it. 创建一个Singleton模式并将您的array放入其中,您可以在您的应用实例中共享您的数组,但请记住这个singleton类将被应用程序销毁,因此您将在其中进行数组。

This is how a singleton is created. 这就是单例的创建方式。

class Bar {
private static class ArrayHolder {
    public static List<String> array = new ArrayList<>();
}

public static Bar getArray() {
    return array;
}
}

Whenever you need to access your array just do this ArrayHolder.getArray() 每当你需要访问你的数组时,只需执行此ArrayHolder.getArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM