日期:2014-05-20 浏览次数:21339 次
List<String> test= new ArrayList<String>();
------解决方案--------------------
用map就行,为什么要自己弄个www,多怪异啊
public static void main(String[] args)
{
List<String> test= new ArrayList<String>();
test.add("a");
test.add("a");
test.add("a");
test.add("b");
test.add("b");
test.add("c");
Map<String,Integer> map=new HashMap<String,Integer>();
for(String str:test)
{
map.put(str, map.get(str)==null?1:map.get(str)+1);
}
System.out.println(map);
}
------解决方案--------------------
for example
List<String> test= new List<String>();
test.add("a");
test.add("a");
test.add("a");
test.add("b");
test.add("b");
test.add("c");
class www{
String a; //元素
String b; //个数
public String toString() {
return String.format("{\"%s\",\"%s\"}", a, b);
}
}
List<www> rusultTest= new List<www>();
for (String s : test) {
boolean found = false;
for (www w : resultTest) {
if (w.a.equals(s)) {
found = true;
w.b = String.valueOf(Integer.valueOf(w.b) + 1);
break;
}
}
if (! found) {
www w = new www();
w.a = s;
w.b = "1";
resultTest.add(w);
}
}
System.out.println(resultTest);
------解决方案--------------------
public static void main(String[] args) {
String[] para = {"a", "b", "c", "a"};
//
List listl = new ArrayList();
Map<String, Integer> mapm = new HashMap<String, Integer>();
for (int i = 0; i < para.length; i++) {
if (mapm.keySet().isEmpty()) {
mapm.put(para[i], 1);
} else if (mapm.containsKey(para[i])) {
mapm.put(para[i], mapm.get(para[i]) + 1);
} else if (!mapm.containsKey(para[i])) {
mapm.put(para[i], 1);
}
}
listl.add(mapm);
System.out.println(listl);
}
结果:
[{b=1, c=1, a=2}]
不知道是不是楼主所要的