5

ポイントのリストのリストがありますList<List<Point>>。例えば:

List<Point> lp;
List<List<Point>> Llp;
Llp.add(lp);

lp私のマトリックス(x = 0)の最初の行になります。Llp行列全体です。

各ポイントの x と y の両方に従って昇順に並べ替えられたリストを取得したい。内部リストのみをソートできましたが、リスト全体はソートできませんでした。誰でも私を助けてもらえますか?ありがとうございました。yに従って内部リスト(つまり、マトリックスの最初の行)をソートするコードは次のとおりです。

private void ord_horiz (List<Point> tab)
{
    boolean ordered_horiz = false;
    int size = tab.size();

    while(!ordered_horiz)
    {
        ordered_horiz = true;

        for(int i=0 ; i < size-1 ; i++)
        {
            if(tab.get(i).y > tab.get(i+1).y)
            {
                swap(tab, i, i+1);
                ordered_horiz = false;
            }
        }
        size--;
    }   
}

private void swap(List<Point> tab, int ind1, int ind2) 
{
    Point c;
    c = tab.get(ind1); 
    tab.set(ind1, tab.get(ind2));
    tab.set(ind2, c);
}

これまでに書いたコードは次のとおりです。

Collections.sort(tab,new Comparator<List<Point>>(){


        @Override
        public int compare(List<Point> o1, List<Point> o2) {
            if(o1 != null && o2 !=null)
            {
                Point var1 = o1.get(0);
                            int var1_x = var1.x;
                            int var1_y = var1.y;
                Point var2 = o2.get(0);
                            int var2_x = var2.x;
                            int var2_y = var2.y;

                            Integer.compare(var1_x, var2_x);
                            Integer.compare(var1_y, var2_y);
                return /* What shall I return ? */;                 
            }
            return 0;
        }
    });

これが私のコンソール出力のスクリーンショットです。その点を整理してほしい。 http://imageshack.us/scaled/large/515/38zy.png

4

1 に答える 1

2

「x が Llp 内の lp の順序であり、y が Llp 内の各 lp の要素の順序であるとすると、x と y に従って外部リストを並べ替えたいと考えています。」

カスタム コンパレータを使用する必要があります。LLP 内の各リストには同じ数の要素があり、各リストは個別の X のみを対象としていると仮定すると、次のように動作するはずです。

//Create the Data

    List<List<Point>> Llp = new ArrayList<List<Point>>();

    for(int i=0; i< 50; i++)
    {
        List<Point> lp = new ArrayList<Point>();

        for(int j=0; j<50; j++)
        lp.add(new Point((int)(Math.random()*1000), i));

        Llp.add(lp);
    }



    // The comparators
    Comparator<List<Point>> comparator_rows = new Comparator<List<Point>>() {

        @Override
        public int compare(List<Point> o1, List<Point> o2) {
            int i = o2.get(0).y;
            int j = o1.get(0).y;
            if (i < j) {
                return 1;
            } else if (i > j) {
                return -1;
            } else {
                return 0;
            }
        }

    };

    Comparator<Point> comparator_columns = new Comparator<Point>() {

        @Override
        public int compare(Point o1, Point o2) {
            int i = o2.x;
            int j = o1.x;
            if (i < j) {
                return 1;
            } else if (i > j) {
                return -1;
            } else {
                return 0;
            }
        }

    };

    // Sort the rows
    Collections.sort(Llp, comparator_rows);

    // Sort the columns
    for (List<Point> element : Llp) 
        Collections.sort(element, comparator_columns);

    //Print the elements
    int rowIndex = 0, columnIndex=0;

    for (List<Point> row : Llp) 
    {
        for (Point element : row) 
        System.out.print("("+element.x+ "," + element.y +")|");

        System.out.println();
    }

上記のコードは、各行の最初の要素を取得し、X 座標を抽出します。次に、この x 座標を使用してすべての行を並べ替えます。

アップデート

データがすべて混同されている場合は、TreeMaps を使用する次のコードを使用して、要素を本来あるべき場所に配置できます。結果はギザギザの配列になります。

//Create the Data

        List<List<Point>> Llp = new ArrayList<List<Point>>();

        for(int i=0; i< 50; i++)
        {
            List<Point> lp = new ArrayList<Point>();

            for(int j=0; j<50; j++)
            lp.add(new Point((int)(Math.random()*1000), (int)(Math.random()*1000)));

            Llp.add(lp);
        }


        //If all data is mixed up we need to filter into new rows based on X using a TreeMap

        TreeMap<Integer, TreeMap<Integer,Point>> data = new TreeMap<Integer,TreeMap<Integer,Point>>();

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            {
                TreeMap<Integer,Point> rowForX;
                if(data.containsKey(element.x))
                    rowForX = data.get(element.x);
                else
                    data.put(element.x, rowForX = new TreeMap<Integer,Point>());    
                    //Create specific row in TreeMap

                rowForX.put(element.y,element);
            }           
        }


        //Convert Sorted TreeMap(s) to Lists

        Llp.clear();
        Iterator<Entry<Integer, TreeMap<Integer, Point>>> it = data.entrySet().iterator();

        while(it.hasNext())
            Llp.add(new ArrayList<Point>(it.next().getValue().values()));


        //Print the elements
        int rowIndex = 0, columnIndex=0;

        for (List<Point> row : Llp) 
        {
            for (Point element : row) 
            System.out.print("("+element.x+ "," + element.y +")|");

            System.out.println();
        }
于 2013-07-11T10:22:42.350 に答える