日期:2014-05-17 浏览次数:20772 次
public class Test { public static void main(String[] args) { String a[]={"sam","sam","liang","zhuo","sam","liang","sam","zhuo","zhuo","liang"}; int[] b = new int[a.length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < i; j++) { if(a[j] == a[i]){ a[i] = a[j]+(b[j]+1); b[j] = b[j]+1; } } } for(int k = 0;k<a.length;k++){ System.out.print(a[k] ); if(k != (a.length-1)){ System.out.print(","); } } } }
------解决方案--------------------
想了好久才想出来
public class Test { public static void main(String[] args) { String[] s={"sam","sam","liang","zhuo","sam","liang","sam","zhuo","zhuo","liang"}; String[] str = new String[s.length]; str[0] = "sam"; for(int i=1;i<s.length;i++){ int count = 0; String ss = s[i]; for(int x=0;x<i;x++){ if(ss.equals(s[x])){//出现了几次 count++; }else{ str[i] = ss; } } str[i] = ss+(count==0?"":count); } for(String x: str){ System.out.println(x); } } }