JAVA/ANDROID 数组转换为LIST
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA/ANDROID 数组转换为LIST相关的知识,希望对你有一定的参考价值。
我有一个数组, String[][] datas = "aaa","aa1","aa11","bbb","bb1","bb11","ccc","cc1","cc11",
"aaa","aa2","aa22","bbb","bb2","bb22","aaa","aa3","aa33",
"bbb","bb3","bb33";
public class DetailModel
public String pron = "";
public List<MeanPhrase> mpList;
//pron = "aaa", mean="aa1", phrase="aa11"
public class MeanPhrase
public String mean, phrase;
想按照"aaa","bbb","ccc"来分组, 转为相应的 LIST<DetailModel>
"aaa","aa2","aa22","bbb","bb2","bb22","aaa","aa3","aa33",
"bbb","bb3","bb33";
Map<String , DetailModel> map = new HashMap<String , DetailModel>();
for (String[] strings : datas)
DetailModel detail = new DetailModel();
detail.setPron(strings[0]);
List<MeanPhrase> li = new ArrayList<MeanPhrase>();
MeanPhrase mean = new MeanPhrase();
mean.setMean(strings[1]);
mean.setPhrase(strings[2]);
li.add(mean);
detail.setMpList(li);
//如果已存在相同的key,如aaa,则在key对应的对象的list中增加元素
if(map.containsKey(detail.getPron()))
map.get(detail.getPron()).getMpList().addAll(li);
else//否则直接新增
map.put(detail.getPron(), detail);
//将map转为list
List<DetailModel> resultList = new ArrayList<DetailModel>(map.values());
for (DetailModel detailModel : resultList)
System.out.println(detailModel);
//输出map中的值以及分组结果记录数
System.out.println("======================================");
Set<String> keySet = map.keySet();
for (String string : keySet)
System.out.println(string+":"+map.get(string).getPron()+":"+map.get(string).getMpList().size());
控制台输出结果:
DetailModel [pron=aaa, mpList=[MeanPhrase [mean=aa1, phrase=aa11], MeanPhrase [mean=aa2, phrase=aa22], MeanPhrase [mean=aa3, phrase=aa33]]]
DetailModel [pron=ccc, mpList=[MeanPhrase [mean=cc1, phrase=cc11]]]
DetailModel [pron=bbb, mpList=[MeanPhrase [mean=bb1, phrase=bb11], MeanPhrase [mean=bb2, phrase=bb22], MeanPhrase [mean=bb3, phrase=bb33]]]
======================================
aaa:aaa:3
ccc:ccc:1
bbb:bbb:3
注意:DetailModel [pron=ccc, mpList=[MeanPhrase [mean=cc1, phrase=cc11]]]输出这样的形式是因为我实体类重写了toString()方法的原因,你那不重写的话输出的应该只是对象地址。本回答被提问者采纳 参考技术B
public class test
public static String[][] datas = "aaa", "aa1", "aa11",
"bbb", "bb1", "bb11", "ccc", "cc1", "cc11",
"aaa", "aa2", "aa22", "bbb", "bb2", "bb22",
"aaa", "aa3", "aa33", "bbb", "bb3", "bb33";
public static List<MeanPhrase> mpList;
// pron = "aaa", mean="aa1", phrase="aa11"
public static class MeanPhrase
public String pron, mean, phrase;
public MeanPhrase(String pron, String mean, String phrase)
this.pron = pron;
this.mean = mean;
this.phrase = phrase;
public static void main(String[] args)
for (int i = 0; i < datas.length; i++)
MeanPhrase meanPhrase = new MeanPhrase(datas[i][0],datas[i][1],datas[i][2]);
mpList.add(meanPhrase);
参考技术C 没什么简便方法 遍历这个数组然后封装到DetailModel类里面,封装好一个就用list.add加一个 参考技术D datas转String
1、下载个Google的gson包,
Gson gson = new Gson();
List<MeanPhrase> list = gson.fromJson(str, new TypeToken<List<MeanPhrase>>().getType());
2、下在json包
使用JSONObject.
java android sqlite将完整的表转换为数组
以上是关于JAVA/ANDROID 数组转换为LIST的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Android 中将 java 字符串转换为 JSON 数组
如何将 JsonArray 对象转换为要在 spinner java android 上使用的字符串数组?
Android:无法转换为 java.lang.CharSequence
ClassCastException:java.lang.Object[] 无法转换为 java.lang.String[] android