问题是:
假设我添加一个数,相应的给Vector也添加一个标志数。
例如:我先添加1,3两个数,vector元素的顺序也是1,3
然后我在1,3中间添加2,vector元素的顺序是1,3,2
我怎么去调整这个顺序啊,使vector的顺序为1,2,3

解决方案 »

  1.   

    调整,根据我项目的具体,我只能调整,而不是添加时进行优先,而是添加完成后,对vector进行调整
      

  2.   

    我写的一个用“快速排序算法”排序的类
    /**
     * 可排序类型
     * @author jiangxu
     * @version 1.0, Jan 25, 2007
     * @since 1.5
     */
    public interface SortableType {  
        /**
         * 与st比较
         * 
         * @param st
         * @return 如果相等返回0; 如果大于st,返回大于0的值;如果小于st返回小于0的值
         */
        int compareTo(SortableType st);  
    }/**
     * 排序
     * @author jiangxu
     * @version 1.0, Jan 25, 2007
     * @since 1.5
     */
    public class Sort {
        /**
         * 快速排序链表中内容
         * 
         * @param <T>
         * @param list 
         */  
        @SuppressWarnings(value={"unchecked"}) 
        public static <T extends SortableType> void quickSort(Vector<T> list){
            if(list.size() <= 1){
                return;
            }
            
            Object[] contentArray = list.toArray();
            
            quickSort(contentArray, 0, contentArray.length-1);
            
            list.clear();
            for(int i=0; i<contentArray.length; i++){
                list.add((T)contentArray[i]); 
            }
        }
        
        /**
         * 
         * @param contentArray
         * @param low
         * @param high
         */
        private static void quickSort(Object[] contentArray, int iLow, int iHigh){
            int low = iLow;
            int high = iHigh;
            SortableType mid = (SortableType)contentArray[(low + high)/2];
            
            do{
                while(((SortableType)contentArray[low]).compareTo(mid) < 0){
                    low ++;
                }
                
                while(((SortableType)contentArray[high]).compareTo(mid) > 0){
                    high--;
                }
                
                if(low <= high){
                    Object tmp = contentArray[low];
                    contentArray[low] = contentArray[high];
                    contentArray[high] = tmp;
                    low++;
                    high--;
                }
                
                if(high > iLow){
                    quickSort(contentArray, iLow, high);
                }
                
                if(low < iHigh){
                    quickSort(contentArray, low, iHigh);
                }
                
            }while(low <= high);
        }    
        public static void main(String[] args){
            ///////////////////////////////////
            //测试对向量中元素的快速排序
            class Data implements SortableType{
                private int value;
                
                public Data(int value){
                    this.value = value;   
                }
                
                public String toString(){
                    return "("+value+")";
                }
                
                public int compareTo(SortableType st){
                    return this.value - ((Data)st).value;
                }
            }
            
            Vector<Data> list = new Vector<Data>();
            list.add(new Data(28));
            list.add(new Data(3));
            list.add(new Data(5));
            list.add(new Data(21));
            list.add(new Data(17));
            list.add(new Data(6));
            list.add(new Data(18));
            list.add(new Data(4));
            list.add(new Data(6));
            list.add(new Data(9));
            list.add(new Data(10));
            list.add(new Data(16));
            list.add(new Data(30));
              
            System.out.println("======快速排序前======");
            for(int i=0; i<list.size(); i++){
                System.out.println(list.get(i));
            }
            
            quickSort(list);
            
            System.out.println("======快速排序后======");
            for(int i=0; i<list.size(); i++){
                System.out.println(list.get(i));
            }
        }
    }
      

  3.   

    两个vector倒一下,中间用个比较
      

  4.   

    对,所以定义一个接口,覆盖compareTo方法用于比较
    public interface SortableType {
    int compareTo(SortableType st);
    }
      

  5.   

    你把要排序的复杂对象继承我定义的SortableType接口,在compareTo(SortableType st)方法里比较两个复杂对象。
    你可以看看我main函数里的例子
      

  6.   

    如题,假设我插入的是:人工活动,自动活动,然后在两个中间插入多实例活动,而这时相应的vector顺序则是人工活动,自动活动,多实例活动,要求的顺序是人工,多实例,自动
      

  7.   

    如题,假设我插入的是:人工活动,自动活动,然后在两个中间插入多实例活动,而这时相应的vector顺序则是人工活动,自动活动,多实例活动,要求的顺序是人工,多实例,自动那你插的时候就用 insert 不要用add ,不就会按照循序插了么?
      

  8.   

    class ActivityBasic implements SortableType{
      public int compareTo(SortableType st){
      
    }
    }
      

  9.   

    本来想写些伪代码来说明,结果习惯了,一按Tab键给发出去了,不写了,我觉得我说的很清楚了,你自己想想吧,你要的任何判断都可以在重载的public int compareTo(SortableType st)函数里实现
      

  10.   

    你想排序总要有个依据吧?除非你的需求是:“A后面如果跟的是B则B后面C在D之前”,这种情况没办法,否则最简单的是,你给每个可能排序的类加个int标志,排在前面的小,排在后面的大,简单比较一下就可以。我看不出你的需求有什么复杂。
    BTW:你在弄工作流?
      

  11.   

    to:abccba9978(gooking)
    谢谢,insert插入时需要一个int 参数的
      

  12.   

    你要是想在链表中插入而不知道索引,用Vector的public int indexOf(Object elem)不就能得到?
      

  13.   

    用Collections的sort方法,自己实现一个比较的Comparatorjava.util.Collectionspublic static <T> void sort(List<T> list,
                                Comparator<? super T> c)Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the list).
    This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place. 
    Parameters:
    list - the list to be sorted.
    c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used. 
    Throws: 
    ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator. 
    UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
    See Also:
    Comparator
      

  14.   

    据你的提示,我想到了一个方法,因为我在插入一个模型时,可以判断它是否有后序模型,若有,则用indexof得到索引号,然后把所有的后序模型后移,在通过insert就可以实现了,下午试一下,谢谢所有回答我问题的人
      

  15.   

    已实现,呵呵,总结,遇到这个问题没有好好思考。在得到索引号后,把索引号之前的元素加到一个新的vector里面,这时再把插入的元素加入到新的vector,然后根据索引号<vector.size()通过elementat得到元素并插入到新的vector里,就这样。呵呵
      

  16.   

    使用STL的sort函数不就行了吗?
      

  17.   

    STL的sort函数就可以。
    sort(v.begin(), v.end());
    这一行就完事儿了。
      

  18.   

    不要想那么复杂,STL做得很复杂了,要是用户再复杂下去,那STL就没有存在的意义了。
      

  19.   

    呵呵,都说了不是sort,也许我的题目说的不是很明确。
    我的贴子第一次上头条,呵呵,改日开贴散分喽