给你个思路,先用Arrays.sort排序,再分组

解决方案 »

  1.   

    如果数组里存的不是数值,是字符串怎么办呢?我写了一个程序,似乎有点问题,可以帮我看看么?/*
     *将一个数组分成几个Vector,相同的数值放在同一个Vector中
     */import java.util.Vector;public class Sort{
    public static void main(String[] args){
    String[] array={"a","b","a","b","c","c","b","c","a","a","c","d","d"};

    Vector sortArray=new Vector(); //存放分组

    Vector v0=new Vector();
    v0.add(array[0]);
    sortArray.add(v0);

    Vector strV=new Vector(); //存放已存在的各不相同的值,找到不同的就放进去
    strV.add(array[0]);
    boolean hasEquals=false;          //是否找到相同的

    int n=0;
    for(int i=1;i<array.length;i++){
    for(n=0;n<strV.size();n++){
    if( array[i].equals((String)strV.elementAt(n)) ){
    ( (Vector)(sortArray.elementAt(n)) ).add(array[i]);
    hasEquals=true;
    //break;
    }
    }
    if(!hasEquals){
    //Vector vnadd1=new Vector();
    //vnadd1.add(array[i]);
    //sortArray.add(vnadd1);
    sortArray.add(new Vector());
    ( (Vector)sortArray.elementAt(n) ).add(array[i]);
    strV.add(array[i]);
    }
    }

    //下面打印测试
    System.out.println("strV.size:"+strV.size());
    System.out.println("sortArray.size:"+sortArray.size());
    for(int j=0;j<strV.size();j++){
    System.out.println( (String)strV.elementAt(j) );
    }
    for(int i=0;i<sortArray.size();i++){
    for(int j=0;j<((Vector)sortArray.elementAt(i)).size();j++){
    System.out.println(i+" "+j+" "+(String)((Vector)sortArray.elementAt(i)).elementAt(j) );
    }
    }

    }
    }
      

  2.   

    Your code is definitely inefficient.This is an extremely simple problem. I give your some tips:
    1. Use Hashtable instead of Vector;
    2. Understanding equals() method in Object.If you think careful above tips, you should know how to do that.