本文由搞代码网为大家展示了《java数组去重代码实例》的代码案例,请各位coder不吝赐教,也欢迎coder点赞收藏转发,感谢
package www.gaodaima.com; public class DiErTi { public static void main(String[] args) { int a[] = { 1, 1, 3, 3, 5, 6, 7, 7 }; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[j] == a[i] && j != i) { a[j] = 0; } } } int count = 0; for (int i = 0; i < a.length; i++) { if (a[i] != 0) { count++; } } int b[] = new int[count]; int j = 0; for (int i = 0; i < a.length; i++) { if (a[i] != 0) { while (j < b.length) { b[j] = a[i]; break; } j++; } } System.out.println("去重结果为:"); for (int i = 0; i < b.length; i++) { System.out.print(b[i] + " "); } } }