我被要求编写一个程序在字符数组中打印值。该数组包含重复的值,但输出不应包含重复的字符。 请勿使用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