原帖:http://topic.csdn.net/u/20100604/23/a83207c7-b9dd-4de2-b6cf-f44209e7bcd3.html 同意用Set来做。但是这里由于觉得可以用正则测试下,所以做法如下. //计算数组中,求绝对值后,不重复的数字个数 /* 例如: 数组:1,2,2,3,4,5,6,-6,-3 输出:6 */ //package com.ricky.www; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Arrays; import static java.lang.Math.abs; public class Test1{ public static void main(String[] args){ int[] primary = {1,2,2,3,4,5,6,-6,-3}; System.out.print("PRIMARY: "); for(int i : primary){ System.out.printf("%-5d", i); } System.out.println(); int[] array = primary.clone(); sort(array); int size = process(array); System.out.println("Total: " + size); } private static void sort(int[] array){ /* 取绝对值后排序 */ if(array == null || array.length == 0){ throw new IllegalArgumentException("array is invalid."); } for(int i = 0 ; i < array.length ; i ++){ array[i] = abs(array[i]); } Arrays.sort(array); } private static int process(int[] array){ String content = Arrays.toString(array); //这里用Arrays.toString(int[] array)得到的String对象包含空格和[] content = content.replaceAll("(//[|//])","").replaceAll(" ",""); String regex = "(//d+)(,//1)+"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); while(matcher.find()){ //这里需要不断的改变content 与 matcher 的值 content = matcher.replaceFirst(matcher.group(1)); matcher = pattern.matcher(content); } String[] result = content.split(","); System.out.print("PROCESS: "); for(String str : result){ System.out.printf("%-5s",str); } System.out.println(); return result.length; } }