java的List操作
list.add("a");list.add("b");list.add("c");list.add("bb);list.add("dd");list.add("a");...
list.add("a");
list.add("b");
list.add("c");
list.add("bb);
list.add("dd");
list.add("a");
根据list中存的元素名称分类显示其共有多少个,如a元素是2个,其他都只1个。元素名称不固定 展开
list.add("b");
list.add("c");
list.add("bb);
list.add("dd");
list.add("a");
根据list中存的元素名称分类显示其共有多少个,如a元素是2个,其他都只1个。元素名称不固定 展开
4个回答
展开全部
List<Integer> list = new ArrayList<Integer>();
list.add("a");
list.add("b");
list.add("c");
list.add("bb);
list.add("dd");
list.add("a");
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(Integer i :list){
map.put(i, map.get(i)+1);
}
for (Object o : map.keySet()) {
System.out.println(o + "出现次数:" + map.get(o));
}
参考一下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.util.Collections;
import java.util.LinkedList;
public class Money
{
public static void main ( String[] args )
{
LinkedList<String> list = new LinkedList<String> ();
list.add ("b");
list.add ("a");
list.add ("b");
list.add ("bb");
list.add ("c");
list.add ("bb");
list.add ("dd");
list.add ("a");
list.add ("a");
list.add ("dd");
list.add ("dd");
list.add ("dd");
list.add ("dd");
list.add ("dd");
String temp = " ," + list.toString ().replaceAll ("[\\[\\]\\s]", "").replaceAll ("[\\,]", ",,") + ", ";
System.out.println (temp);
String had = ",";
for ( String string : list )
{
if (had.indexOf ("," + string + ",") == -1)
{
System.out.println (string + ": " + ( temp.split ("\\," + string + "\\,").length - 1 ));
had += string + ",";
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("bb");
list.add("dd");
list.add("a");
System.out.println();
Map<String, Integer> counts = new HashMap<String, Integer>();
for (String s : list) {
Integer c = counts.get(s);
if (c == null) {
c = 0;
}
counts.put(s, c + 1);
}
System.out.println("元素数量:");
for (String k : counts.keySet()) {
System.out.println(k + " " + counts.get(k));
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
List<String> container = new ArrayList<String>();
Map<String,Integer> counters = new HashMap<String,Integer>();
for(String i : list){
if(!container.contains(i)){
container.add(i);
counters.put(i,1);
}else{
counters.put(i,(counters.get(i)+1));
}
}
for(String key : counters.keySet()){
System.out.println(“含”+key+" : "+counters.get(key)+"个");
}
Map<String,Integer> counters = new HashMap<String,Integer>();
for(String i : list){
if(!container.contains(i)){
container.add(i);
counters.put(i,1);
}else{
counters.put(i,(counters.get(i)+1));
}
}
for(String key : counters.keySet()){
System.out.println(“含”+key+" : "+counters.get(key)+"个");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询