java中声明一个类People,成员变量有姓名,出生日期,性别,身高,体重等;生成10个类对象,并放在一个一维数组中,编写方法按身高进行排序
时间: 2024-03-26 14:35:21 浏览: 142
好的,这里是Java语言的代码示例:
```java
import java.util.Arrays;
import java.util.Comparator;
public class People {
private String name;
private String birthday;
private String gender;
private int height;
private int weight;
public People(String name, String birthday, String gender, int height, int weight) {
this.name = name;
this.birthday = birthday;
this.gender = gender;
this.height = height;
this.weight = weight;
}
public static void main(String[] args) {
People[] peopleList = {
new People("张三", "1990-01-01", "男", 175, 65),
new People("李四", "1992-02-02", "女", 170, 60),
new People("王五", "1995-03-03", "男", 180, 70),
new People("赵六", "1998-04-04", "女", 165, 50),
new People("钱七", "2000-05-05", "男", 185, 75),
new People("孙八", "2002-06-06", "女", 160, 45),
new People("周九", "2005-07-07", "男", 190, 80),
new People("吴十", "2008-08-08", "女", 155, 40),
new People("郑十一", "2010-09-09", "男", 195, 85),
new People("冯十二", "2012-10-10", "女", 150, 35)
};
Arrays.sort(peopleList, Comparator.comparing(People::getHeight));
for (People p : peopleList) {
System.out.println(p.getName() + " " + p.getHeight());
}
}
public String getName() {
return name;
}
public String getBirthday() {
return birthday;
}
public String getGender() {
return gender;
}
public int getHeight() {
return height;
}
public int getWeight() {
return weight;
}
}
```
以上代码中,我们定义了一个`People`类,包含了姓名、出生日期、性别、身高和体重等属性。我们创建了一个包含10个`People`对象的数组`peopleList`,并使用`Arrays.sort()`方法按照身高进行排序,返回一个新的排序后的数组。最后我们遍历打印排序后的数组中每个人的姓名和身高。
希望能对您有所帮助!
阅读全文