gpt4 book ai didi

java - Android 应用程序中 ArrayList 的 OutOfMemoryError

转载 作者:太空狗 更新时间:2023-10-29 16:21:41 26 4
gpt4 key购买 nike

在一个 Activity 中,我编写了可以正常工作的代码。

但现在我已经使用以下代码向此 Activity 添加了一个方法:

    private void obtenerDatosReuniones(){

try {

int j=0;

String aux = jsonReuniones.getString("nombres");

String aux2 = null;

aux2 = aux.replace("[", "");

aux2= aux2.replace("]", "");

String [] campos = aux2.split(",");

while(j<campos.length){

nombres_reuniones.add(campos[j]);

}

nombres_reunones 的类型是 ArrayList

当我运行应用程序时,nombres_reuniones.add (campos [j]) 行出现以下错误:

我做错了什么?

谢谢!

最佳答案

看看你的循环:

while(j<campos.length){
nombres_reuniones.add(campos[j]);
}

您如何预期完成?您不修改 j。鉴于您在声明 j 并在一开始就为其分配值 0 后没有对它进行任何更改,这将是很多 更清晰:

for (int j = 0; j < campos.length; j++) {
nombres_reuniones.add(campos[j]);
}

或者更好:

for (String item : campos) {
nombres_reuniones.add(item);
}

或者更简单:

nombres_reunions.addAll(Arrays.asList(campos));

此外,您之前的代码可以更简单。看看这个:

String aux2 = null;
aux2 = aux.replace("[", "");
aux2= aux2.replace("]", "");

为什么要为 aux2 分配一个 null 的初始值,然后立即覆盖它?此外,您可以轻松地链接方法调用。它会更整洁:

String aux2 = aux.replace("[", "").replace("]", "");

事实上,您可以从头到尾将整个字符串操作链接在一起:

String[] campos = jsonReuniones.getString("nombres")
.replace("[", "")
.replace("]", "")
.split(",");
nombres_reunions.addAll(Arrays.asList(campos));

(我会停在那里,而不是内联那个表达式......)

关于java - Android 应用程序中 ArrayList 的 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944044/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com