如何使用JAVA在Android中拆分ArrayList()

千金一刻莫空度,老大无成空自伤。这篇文章主要讲述如何使用JAVA在Android中拆分ArrayList?相关的知识,希望能为你提供帮助。
这是将值放在ArrayList中的代码,我无法用“,”分割arraylist。有人可以帮助我如何完成这项任务?

spinnerArrayList = new ArrayList< String> (); spinnerArrayList.add(menuFieldInstance.getFieldValues()); Log.i("spinnerArrayList",""+spinnerArrayList); //for(int j=0; j< spinnerArrayList.size(); j++) //{ Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(",")); //}

这是拆分后的Spinner ArrayList和SpinnerArrayList的Logcat .............
02-10 22:00:48.285:I / spinnerArrayList(19378):[0100~Avon&Somerset,0200~Bedfordshire,0300~Cambridgeshire,0400~Kheshire,0500~伦敦金融城,0600~Cleveland,0700~Cumbria,0800 ?德比郡,0900~德文郡和康沃尔郡,1000~多塞特,1100~达勒姆,1200~艾塞克斯,1300~格洛斯特郡,1400~大曼彻斯特,1500~汉普郡,1600~赫特福德郡,1700~亨伯赛德,1800~肯特,1900~兰开夏郡, 2000~莱斯特郡,2100~Linconshire,2200~默西塞德郡,2300~大都会,2400~诺福克,2500~北安普敦郡,2600~诺桑比亚,2700~北约克郡,2800~诺丁汉郡,2900~南约克郡,3000~斯塔福德郡,3100~Suffolk, 3200~Surrey,3300~Sossex,3400~Thames Valley,3500~Warwickshire,3600~West Mercia,3700~West Midlands,3800~West Yorkshire,3900~Wiltshire,4000~Dyfed,4100~Gwent,4200~Worth Wales,4300 ?南威尔士,4400~皇家阿尔斯特,4500~Strathclyde,4600~苏格兰中部,4700~Dumfries和Galloway,4800~Fife,4900~Grampian,5000~Lothian和Borders,5100~苏格兰北部,5200~Tayside,5300~Gurnsey ,5400~泽西州,5500~马恩岛,NO~No Poli ce响应,THAM~THEMES VALLEY,WEST~WEST MIDLANDS POLICE,5600~Buckinghamshire] 02-10 22:00:48.285:I / spinnerArrayList分裂后,(19378):[Ljava.lang.String; @ 41b9a498
答案
// try to print this way then you getting actual value at index becz your try to print String[] object rather each index value so do this way spinnerArrayList = new ArrayList< String> (); spinnerArrayList.add(""); for (int i=0; i< spinnerArrayList.size(); i++){ String[] splitedValue = https://www.songbingjia.com/android/spinnerArrayList.get(i).split(","); for (int j=0; j< splitedValue.length; j++){ Log.i(i+" at ArrayIndex "+j+" at splitedIndex Value is > > ",splitedValue[j]); String[] splitedValue1 = splitedValue[j].split("~"); if(splitedValue1.length==1){ continue; } for (int k=0; k< splitedValue1.length; k++){ Log.i(j+" at splitedIndex "+k+" at splited1Index Value is > > ",splitedValue1[k]); } }}

另一答案你不能拆分ArrayList。你可以拆分String,例如"I am some String, you can split me".split(",")将返回一个包含2个字符串的数组,但是ArrayList是一个包含一些字符串的数据结构,并不意味着它们用逗号分隔。您可以尝试拆分列表中的每个项目,例如
for (String s : spinnerArrayList) { String[] res = s.split(","); // do smth with res }

另一答案spinnerArrayList.get(0).split(",")不会拆分“arrayList”,它会在索引零处拆分字符串,并且由于返回的结果是字符串数组 - 您无法使用+运算符将其连接到另一个字符串:
Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(","));

首先将splitted字符串读入String数组:
String[] arr = spinnerArrayList.get(0).split(",");

【如何使用JAVA在Android中拆分ArrayList()】然后循环数组并打印值“
for(String val: arr){ Log.i(val); }


    推荐阅读