Java每日一题Java 杂谈代码改变世界

Java每日一题20161031

2016-10-31  本文已影响87人  weknow

20161028问题解析请点击今日问题下方的“【Java每日一题】20161031”查看(问题解析在公众号首发,公众号ID:weknow619)

package Oct2016;

import java.util.SortedSet;
import java.util.TreeSet;

public class Ques1031 {

    public static void main(String[] args) {
        SortedSet<People> set = new TreeSet<People>();
        set.add(new People(170));
        set.add(new People(165));
        
        for(People people : set){
            System.out.println("身高:"+people.getHeight());
        }
        System.out.println();
        
        // 将身高矮的人变高
        set.first().setHeight(175);  // 之前排在第一位的人就是最矮的
        for(People people : set){
            System.out.println("身高:"+people.getHeight());
        }
    }
}

class People implements Comparable<People>{

    private int height;
    
    public People(int height) {
        super();
        this.height = height;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public int compareTo(People p) {
        // 按身高从矮到高排序
        return height - p.height;
    }
}

今日问题:请问主程序输出结果是什么?(点击以下“【Java每日一题】20161031”查看20161028问题解析)

上一篇下一篇

猜你喜欢

热点阅读