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>
最新回答
安陵忻美

2024-09-26 21:47:29

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()方法的原因,你那不重写的话输出的应该只是对象地址。
陌生的挚爱

2024-09-26 03:58:50


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);

}

}

}

初心不负

2024-09-26 04:50:11

没什么简便方法 遍历这个数组然后封装到DetailModel类里面,封装好一个就用list.add加一个
冭儍冭迗眞

2024-09-26 13:47:54

datas转String
1、下载个Google的gson包,
Gson gson = new Gson();
List<MeanPhrase> list = gson.fromJson(str, new TypeToken<List<MeanPhrase>>(){}.getType());
2、下在json包
使用JSONObject.