JAVA中对String数组根据字符长度进行分组并且排序
答案:2 悬赏:50 手机版
解决时间 2021-04-05 21:13
- 提问者网友:wodetian
- 2021-04-04 21:59
JAVA中对String数组根据字符长度进行分组并且排序
最佳答案
- 五星知识达人网友:老鼠爱大米
- 2021-04-04 22:45
private static List stringGroupO(String[] strs){
List list1 = new ArrayList();
List list2 = new ArrayList();
for(int i=0;i Map param = new HashMap();
param.put(strs[i].length(), strs[i]);
list1.add(param);
list2.add(strs[i].length());
}
List newList = new ArrayList(new HashSet(list2));
List resultList = new ArrayList();
for(int j =0;j List list = new ArrayList();
for(int k=0;k Map map = (Map)list1.get(k);
if(map.get(newList.get(j)) != null){
list.add(map.get(newList.get(j)));
}
}
resultList.add(list);
}
return resultList;
}
List list1 = new ArrayList();
List list2 = new ArrayList();
for(int i=0;i
param.put(strs[i].length(), strs[i]);
list1.add(param);
list2.add(strs[i].length());
}
List newList = new ArrayList(new HashSet(list2));
List resultList = new ArrayList();
for(int j =0;j
for(int k=0;k
if(map.get(newList.get(j)) != null){
list.add(map.get(newList.get(j)));
}
}
resultList.add(list);
}
return resultList;
}
全部回答
- 1楼网友:煞尾
- 2021-04-04 23:42
好像也很复杂...
String[] str = {"1", "2", "3", "66", "54", "513", "323", "12", "121"};
Map> map = new HashMap>(); //按照长度区分数据
Set indexSet = new HashSet(); //长度去重
for (String string : str) {
int length = string.length();
List list = map.get(length);
if (list == null) {
list = new ArrayList();
}
list.add(string);
Collections.sort(list); //默认排序
indexSet.add(length);
map.put(length,list);
}
List indexList = new ArrayList();
for (Integer integer : indexSet){
indexList.add(integer);
}
Collections.sort(indexList); //长度默认排序
JSONArray jsonArray = new JSONArray();
for (Integer integer : indexList){
List valueList = map.get(integer);
String[] strings = valueList.toArray(new String[valueList.size()]);
jsonArray.add(strings);
}
System.out.println(jsonArray.toString());
String[] str = {"1", "2", "3", "66", "54", "513", "323", "12", "121"};
Map
Set
for (String string : str) {
int length = string.length();
List
if (list == null) {
list = new ArrayList
}
list.add(string);
Collections.sort(list); //默认排序
indexSet.add(length);
map.put(length,list);
}
List
for (Integer integer : indexSet){
indexList.add(integer);
}
Collections.sort(indexList); //长度默认排序
JSONArray jsonArray = new JSONArray();
for (Integer integer : indexList){
List
String[] strings = valueList.toArray(new String[valueList.size()]);
jsonArray.add(strings);
}
System.out.println(jsonArray.toString());
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯