gpt4 book ai didi

java - 即使列表中没有项目,Android ArrayList 大小也返回 1

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:08 26 4
gpt4 key购买 nike

我正在使用 Android。我有一个字符串数据的 ArrayList,我将整个 Arraylist 作为单个字符串保存在本地数据库中。稍后我从数据库中获取这个字符串并将这个字符串转换为数组列表,

案例一:

  String dbString=[10,5,34,67];  // array list as string
String replace = dbString.toString().replace("[", "");
String replace1 = replace.replace("]", "");
mysignimgList = new ArrayList<String>(Arrays.asList(replace1.split(",")));

案例二:

  String dbString2=[];  //empty array list as string
String replace = dbString2.toString().replace("[", "");
String replace1 = replace.replace("]", "");
mysignimgList2 = new ArrayList<String>(Arrays.asList(replace1.split(",")));

但在第二种情况下,即使 arraylist 中没有项目,它的大小也会变成 mysignimgList2.size()=1。

最佳答案

这是 String.split 的一个特征.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.

因此拆分一个空字符串会返回一个包含一个空字符串的 1 项数组。

关于java - 即使列表中没有项目,Android ArrayList 大小也返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34895686/

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