java中去掉字符串数组中重复的字符串(不改变原有顺序)并计数(不同的字符串重复了多少次)

如题所述

String[] arr = {"abc","bbc","abc","def"};
List<String> list =new ArrayList<String>();
String oldStr = "";
for (String currStr : arr) {
    if (oldStr.contains(currStr + ",")) { //已有跳过
        continue;
    }
    list.add(currStr);
    oldStr += currStr + ",";
    int count = 0;
    for (String anArr : arr) {
        if (anArr.equals(currStr)) {
            count++;
        }
    }
    System.out.println(currStr + "|" + count);
}
String[] strings = list.toArray(new String[list.size()]);

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-12-30
String[] str = {"aa","bb","cc","cc","bb"};
List<String> list = new ArrayList<>();
for (int i=0; i<str.length; i++) {    
        if(!list.contains(str[i])) {    
            list.add(str[i]);    
        }    
    }    
System.out.println("去除重复后的list集合"+list);

for(int i=0;i<list.size();i++){
    int count = -1;
    for(int j=0; j<str.length; j++){
        if(list.get(i).equals(str[j])){
            count ++;
        }
    }
    System.out.println(list.get(i)+"共重复了"+count+"次");
}

第2个回答  2015-02-08
小兄弟,你的这个是什么意思?

能不能说的在明确一点;

如果对回答满意,请点【采纳答案】,如果还有问题,请点【追问】

希望我的回答对您有所帮助,希望能采纳。追问

就是一个字符串数组,去掉其中重复的,如:ab cde ab ab f ab ,去掉后为ab cde f ,其中ab重复3次,cde 1次,f 1次
用java编程实现

本回答被网友采纳
相似回答