JAVA对于对象排序的最简单应用,看看还能不能用更高效,更简练的方法完成此程序?

为了要对某一批区间对进行排序,并将多余的, 已经在集合中的“一对区间”进行去除。

下面程序是完全是个人创作:

package musbox;

import java.util.Collections;
import java.util.Vector;

public class SectionMap {
    public int capacity = 0;
    public Vector<Section> fills;//,emptys;
    public Temple reducingTask;
    
    public SectionMap (int cap){
        capacity = cap;
        fills  = new Vector<Section>();
//        emptys = new Vector<Section>();
    }
    
    
    @SuppressWarnings(”unchecked”)
    private void reduce(){
        for (int i=0;i<fills.size();i++){            
            System.out.print(fills.get(i).begin+”,”+fills.get(i).end+”:”);
        }
        System.out.println();
        Collections.sort(fills);
        for (int i=0;i<fills.size();i++){            
            System.out.print(fills.get(i).begin+”,”+fills.get(i).end+”:”);
        }
        System.out.println();
        
        for (int i=1;i<fills.size();i++){
            Section a = fills.get(i-1);
            Section b = fills.get(i);
            
            if (a.begin<=b.begin && a.end>=b.end){
                fills.remove(b);
                i–;
                continue;
            }
            
            if (a.begin<=b.begin && a.end<=b.end && a.end>=b.begin){
                Section c = new Section(a.begin,b.end);
                fills.remove(a);
                fills.remove(b);
                fills.add(i-1,c);
                i–;
                continue;
            }    
        }
    }
    
    
    public boolean add(int begin,int end){
        if (begin<capacity && end<capacity){
            fills.add(new Section(begin,end));
            return true;
        }
        return false;
    }
    
    
    public int[] getSectionPairs(){
        reduce();
        int[] pairs = new int[fills.size()*2];
        for (int i=0;i<fills.size();i++){
            pairs[i*2]=fills.get(i).begin;
            pairs[i*2+1]=fills.get(i).end;
        }
        return pairs;
    }

    public void show(){
        reduce();
    
        for (int i=0;i<fills.size();i++){            
            System.out.print(fills.get(i).begin+”,”+fills.get(i).end+”:”);
        }
        System.out.println();
    }
    
    
    class Section implements Comparable{
        private int begin;
        private int end;
        
        public Section(int begin,int end){
            this.begin    = Math.min(begin, end);
            this.end    = Math.max(begin, end);            
        }
        
        
        public int compareTo(Object anotherSection) {
            Section r = (Section)anotherSection;
            
            if (this.begin>=r.begin)            
                return     1;
            else if (this.begin<r.begin)
                return -1;
            else    
                return 0;
        }
    }
    
    
    public static void main(String[] args) throws InterruptedException {
        SectionMap t= new SectionMap(10000);
        
        for (int i=0;i<10000;i++){
            int c = (int)(Math.random()*1000*i);
            t.add(c,c+(int)(Math.random()*100));
        }
        t.add(2001, 5000);
        
        t.add(5, 10);
        
        t.add(101, 500);
        t.add(0, 100);
        t.add(1050, 1200);
    
        
        int[] p = t.getSectionPairs();
        for (int i=0;i<p.length;i++){            
            System.out.print(p[i]+”,”);
        }
//        t.show();            
    
        
    
        
        
        

    }

}

continue reading.....