我被要求編寫一個程序在字符數組中打印值。該數組包含重複的值,但輸出不應包含重複的字符。 請勿使用Set。這是我創造的。讓我知道是否有其他有效的方法來做同樣的事情。如何在字符數組中打印字符而不重複?
public class RemoveDuplication {
public static void main (String[] args){
char[] a= new char[]{'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c'};
String s=Character.toString(a[0]);
for (int i=1; i<a.length; i++) {
if ((s.indexOf(a[i])) == -1) {
s = s + Character.toString(a[i]);
} else {
}
}
// if you want character array as result
char[] result = s.toCharArray();
System.out.println(result);
}
}
'System.out.println(Arrays.toString(result));' –
嗨艾略特,感謝您的建議。 請注意,我們重載了'println'方法,它接受char數組作爲參數並打印char數組中的值列表。 – Vicky