和
public static String[][] Name = new String[1000][1000];
您正在分配 100 万个字符串 (1000x1000),这是相当多的。如果此页面上的信息正确,则每个字符串至少需要 40 个字节,因此在您的情况下大约为 39 MB,对于 1 个活动来说,这很容易导致堆上的内存过多。如果您只使用 10,我会从那里开始重构。可能有比您的方法更好的解决方案,但是如果没有关于您的代码的更多细节,很难给出它们。但是在我的脑海中,为什么不使用Set<String>
or List<String>
?
编辑:所以在我看来,您只想要一个动态扩展的集合。因为那个数组不是最好的选择。有许多数据类型,但一个简单的例子是,ArrayList
它也使用数组作为支持数据类型,但默认情况下将以 10 的容量实例化,如果您继续添加元素,则会动态扩展
List<String> stringList = new ArrayList<String>();
stringList.add("string1");
stringList.add("string2");
...
如果您希望每个元素都有自己的字符串列表,只需为此创建一个对象:
public class CompoundString {
private String key;
private List<String> stringList;
...
}
并像这样使用它
List<CompoundString> compoundStringList = new ArrayList<CompoundString>();
compoundStringList.add(new CompoundString("string1", new ArrayList<String>());
或者只使用地图:
Map<String,List<String>> stringMap = new HashMap<String,List<String>>();
stringMap.put("string1", new ArrayList<String>());
这是大多数编程语言中非常基本的概念,我会开始阅读一些关于各种集合的文档: