本帖最后由 java2000_net 于 2008-10-25 00:08:44 编辑

解决方案 »

  1.   

    与正则有嘛关系呢?用Hashtable就行了吧,主键就是绝对值,值就是频率计数
      

  2.   

    和正则没关系int [] a = {5,0,-5,2,-4,5};
    Map<Integer, Integer> m = new HashMap<Integer, Integer>();
    for(int i = 0; i < a.length; i++){
    if(m.containsKey(Math.abs(a[i]))){
    m.put(Math.abs(a[i]), m.get(Math.abs(a[i]))+1);
    }else{
    m.put(Math.abs(a[i]), 1);
    }
    }
    System.out.println(m);
      

  3.   

    ////////////还是5搂的好!自己写的太麻烦了public class MyAbs {
        
    static int[] sum;

    //交换
    static void swap(int[] a,int m,int n){
    int t=a[m];
    a[m]=a[n];
    a[n]=t;
    }

    //快排
    static void Qsort(int[]a,int m,int n){

    if(m<n){
    int i=m;
    int j=n;
    int k=(i+j)/2;
    while(i<j){
    while(a[i]<a[k])i++;
    while(a[j]>a[k])j--;
    if(i<j){
    swap(a,i,j);
    }
    }
    swap(a,k,j);
    Qsort(a,m,j);
    Qsort(a,j+1,n);
    }
    }

    //统计频数大于1
    static void func (int[] a){
    for(int i=0;i<a.length;i++){
    sum[Math.abs(a[i])]++;
    }
    for(int i=0;i<sum.length;i++){
    if(sum[i]!=0){
    System.out.println(i+"出现频数为"+sum[i]);
    }

        }
    }

    public static void main(String[] args) {
        
    int[] a=new int[]{5,0,-5,2,-4,5};
    int max=0;
    for(int i=0;i<a.length;i++){
    if(max<Math.abs(a[i])){
    max=Math.abs(a[i]);
    }
    }
    sum=new int[max+1];
    Qsort(a,0,a.length-1);
        func(a);
    }
    }
      

  4.   


    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    public class TestPingLv{
        public static void main(String[] args){
         //把从数组放list中接收
         int[] a = {5,0,-5,2,-4,5,4,4,5};
         List<Integer> sortList = new ArrayList<Integer>();
            for(int i = 0 ; i < a.length ; i++){
             sortList.add(Math.abs(a[i]));
            }
         // 5 0 5 2 4 5 
            
            Collection<Integer> c = new ArrayList<Integer>();
           
         for(int i = 0 ; i < sortList.size() ; i++){
         int x = 0;
         for(int j = 0 ; j < sortList.size() ; j++){
         if(sortList.get(i).equals(sortList.get(j))){
         x++;
         c.add(sortList.get(j));
         }
         }
         System.out.println(sortList.get(i) +  " 出现 " + x + " 次");
         sortList.removeAll(c);
         }
         for(int i = 0 ; i < sortList.size() ;i++){
         System.out.println(sortList.get(i) + " 出现 1 次" );
         }
        }
    }