使用LinkedHashMap来把字符串中每种字符的下标存储到对应的集合中。利用LinkedHashMap的有序性(提取元素和添加元素的顺序一致)来直接生成答案public static void printIndexes(String str){ LinkedHashMap> IndexSet = new LinkedHashMap<>(); for(int i=0;i Character c = str.charAt(i); if(!IndexSet.containsKey(c)){ IndexSet.put(c, new ArrayList()); } IndexSet.get(c).add(i); } StringBuilder sb = new StringBuilder(); for(char key:IndexSet.keySet()){ List values = IndexSet.get(key); if(values.size()>1){ for(int i:values) { sb.append(key+","+i+","); } } } sb.deleteCharAt(sb.length()-1); System.out.println(sb); } String str = "afcacbcbcki"; printIndexes(str); a,0,a,3,c,2,c,4,c,6,c,8,b,5,b,7