第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何編寫(xiě)比較多個(gè)參數(shù)的比較器?

如何編寫(xiě)比較多個(gè)參數(shù)的比較器?

躍然一笑 2021-11-17 15:04:02
我正在嘗試編寫(xiě)一個(gè)比較器來(lái)比較 Coordinate 類(lèi)的兩個(gè)對(duì)象。Coordinate 類(lèi)非常簡(jiǎn)單:public class Coordinate {    private int x, y;    public Coordinate(int x, int y) {        this.x = x;        this.y = y;    }    public int getX() {        return x;    }    public void setX(int x) {        this.x = x;    }    public int getY() {        return y;    }    public void setY(int y) {        this.y = y;    }}現(xiàn)在我想讓 Comparator 比較 Coordinate 類(lèi)的兩個(gè)實(shí)例的 x 和 y 值。這是一個(gè)例子:我有一個(gè)坐標(biāo) c1,它有 x = 42 和 y = 23。我的第二個(gè)坐標(biāo) c2 有 x = 23 和 y = 54。現(xiàn)在我把它們都放在一個(gè) ArrayList 中,想要對(duì)列表進(jìn)行排序。我想按以下方式排序:具有最低 y 值的坐標(biāo)始終排在第一位,當(dāng)您有兩個(gè)具有相同 y 值的坐標(biāo)時(shí),該坐標(biāo)排在第一位,其 x 值較低。例子:c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)  那么我怎樣才能為此目的編寫(xiě)一個(gè)比較器呢?
查看完整描述

2 回答

?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)

                                     .thenComparingInt(Coordinate::getX);

您可以通過(guò)thenComparing和構(gòu)建復(fù)合比較器thenComparingX。


var list = List.of(

        new Coordinate(6, 4),

        new Coordinate(2, 5),

        new Coordinate(5, 4)

);


list.sort(c);

System.out.println(list);

片段打印


[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]


查看完整回答
反對(duì) 回復(fù) 2021-11-17
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

使用比較器



import java.util.ArrayList;

import java.util.Comparator;


class Coordinate {

    private int x, y;


    public Coordinate(int x, int y) {

        this.x = x;

        this.y = y;

    }


    public int getX() {

        return x;

    }


    public void setX(int x) {

        this.x = x;

    }


    public int getY() {

        return y;

    }


    public void setY(int y) {

        this.y = y;

    }


    public String toString() {

        return "x = " + x + " y = " + y;

    }

}


public class Temp {

    public static void main(String[] args) {

        ArrayList<Coordinate> A = new ArrayList<>();

        A.add(new Coordinate(1, 2));

        A.add(new Coordinate(2, 1));

        A.add(new Coordinate(3, 2));

        A.sort(new Comparator<Coordinate>() {


            @Override

            public int compare(Coordinate o1, Coordinate o2) {

                if (o1.getY() < o2.getY()) {

                    return -1;

                } else if (o1.getY() > o2.getY()) {

                    return 1;

                } else {

                    if (o1.getX() < o2.getX()) {

                        return -1;

                    } else if (o1.getX() > o2.getX()) {

                        return 1;

                    }

                    return 0;

                }

            }

        });

        System.out.println(A.toString());

    }

}

使用可比較的接口



import java.util.ArrayList;


class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface

    private int x, y;


    public Coordinate(int x, int y) {

        this.x = x;

        this.y = y;

    }


    public int getX() {

        return x;

    }


    public void setX(int x) {

        this.x = x;

    }


    public int getY() {

        return y;

    }


    public void setY(int y) {

        this.y = y;

    }


    @Override

    public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface

        if (y < o.y) {

            return -1; 

        } else if (y > o.y) {

            return 1;

        } else {

            if (x < o.x) {

                return -1;

            } else if (x > o.x) {

                return 1;

            }

            return 0;

        }

    }


    public String toString() {

        return "x = " + x + " y = " + y;

    }

}


public class Temp {

    public static void main(String[] args) {

        ArrayList<Coordinate> A = new ArrayList<>();

        A.add(new Coordinate(1, 2));

        A.add(new Coordinate(2, 1));

        A.add(new Coordinate(3, 2));

        A.sort(null);

        System.out.println(A.toString());

    }

}



輸出


[x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]


查看完整回答
反對(duì) 回復(fù) 2021-11-17
  • 2 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)