- // 数组去重复,例如: 原始数组是{4,2,4,6,1,2,4,7,8},得到结果{4,2,6,1,7,8}
- public class Test18 {
- public static void main(String[] args) {
- int[] arr = { 4, 2, 4, 6, 1, 2, 4, 7, 8 };
- show(arr);
- }
- private static void show(int[] arr) {
- StringBuilder sb = new StringBuilder();
- // 将数组添加到sb对象中;
- for (int i : arr) {
- sb.append(i);
- }
- // 输出sb为424612478
- System.out.println(sb);
- int i = sb.length() - 2;
- int count = sb.length() - 1;
- // 循环判断去重复
- while (i >= 0) {
- while (count > i) {
- if (arr[i] == arr[count])
- sb.deleteCharAt(count);
- count--;
- }
- i--;
- count = sb.length() - 1;
- }
- // 为什么输出42617 而不是426178
- System.out.println(sb);
- }
- }
复制代码
|
|