Ⅰ 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"}};
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()方法的原因,你那不重写的话输出的应该只是对象地址。
Ⅱ java中数组如何转换成List
//小例子
//这里注意的是,不能是基本类型,必须是包装类Byte[]可以。
publicclassDemo{
publicstaticvoidmain(String[]args)throwsException{
String[]arr={"asd","sd","df"};
Listlist=Arrays.asList(arr);
System.out.println(list);
}
}
Ⅲ Java中有将二维数组转化为二维的List的方法吗即ArrayList<ArrayList<Object>>的形式
没有,二维数组那就用循环遍历吧。
publicstaticvoidmain(String[]args)//NOCHECKSTYLE
{
intm=4;
intn=5;
Integerarray[][]=newInteger[m][n];
List<Integer[]>list_array=Arrays.asList(array);
List<List<Integer>>list_integer=newArrayList<List<Integer>>();
for(inti=0;i<list_array.size();i++)
{
list_integer.add(Arrays.asList(list_array.get(i)));
}
for(List<Integer>innerList:list_integer)
{
for(Integervalue:innerList)
{
System.out.print(value+"");
}
System.out.println();
}
}