如何在java android中以字符串形式动态存储值?
在我的项目中,我需要将值动态存储在一个字符串中,并需要将该字符串拆分为“,”。我该怎么做?请帮帮我 我的代码:如何在java android中以字符串形式动态存储值?,android,Android,在我的项目中,我需要将值动态存储在一个字符串中,并需要将该字符串拆分为“,”。我该怎么做?请帮帮我 我的代码: static ArrayList<ArrayList<String>> listhere; ArrayList<String> arropids; String arropids1; for(int q=0;q<listhere.size();q++) { arr
static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1;
for(int q=0;q<listhere.size();q++)
{
arropids = listhere.get(q);
if(arropids.get(3).equals("1"))
{
arropids1 += arropids.get(0) + ",";
System.out.println("arropids1"+arropids1);
}
}
static ArrayList listhere;
ArrayList arropids;
字符串arropids1;
对于(int q=0;q您必须获得NullPointerException,因为您尚未初始化字符串,请将其初始化为
String arropids1="";
它将解决您的问题,但我不建议将字符串用于此任务,因为字符串是不可变类型,您可以为此使用StringBuffer,因此我建议使用以下代码:
static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
StringBuffer buffer=new StringBuffer();
for(int q=0;q<listhere.size();q++)
{
arropids = listhere.get(q);
if(arropids.get(3).equals("1"))
{
buffer.append(arropids.get(0));
buffer.append(",");
System.out.println("arropids1"+arropids1);
}
}
为了在for循环中存储解析后分割结果,可以对存储的字符串使用split方法,并将其设置为如下所示的字符串数组:
static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1 = "";
for(int q=0;q<listhere.size();q++) {
arropids = listhere.get(q);
if(arropids.get(3).equals("1"))
{
arropids1 += arropids.get(0) + ",";
System.out.println("arropids1"+arropids1);
}
}
String[] results = arropids1.split(",");
for (int i =0; i < results.length; i++) {
System.out.println(results[i]);
}
static ArrayList listhere;
ArrayList arropids;
字符串arropids1=“”;
对于(int q=0;qSo),您想存储从数据中解析出来的每个arropids1吗?是的……存储后,我想拆分每个arropids1。。。
static ArrayList<ArrayList<String>> listhere;
ArrayList<String> arropids;
String arropids1 = "";
for(int q=0;q<listhere.size();q++) {
arropids = listhere.get(q);
if(arropids.get(3).equals("1"))
{
arropids1 += arropids.get(0) + ",";
System.out.println("arropids1"+arropids1);
}
}
String[] results = arropids1.split(",");
for (int i =0; i < results.length; i++) {
System.out.println(results[i]);
}