创建一个Person类,有以下属性:名字、身高、性别、年龄,创建10个对象,用java程序
1、创建一个Person类,有以下属性:名字、身高、性别、年龄,创建10个对象,放到数组或LIST中,以身高的降序进行排序并输出回答对后有高分线上急...
1、 创建一个Person类,有以下属性:名字、身高、性别、年龄,创建10个对象,放到数组或LIST中,以身高的降序进行排序并输出
回答对后 有高分线上 急 展开
回答对后 有高分线上 急 展开
3个回答
展开全部
//用到了冒泡排序
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Person A = new Person("A", 178, "男", 18);
Person B = new Person("B", 156, "女", 18);
Person C = new Person("C", 171, "男", 20);
Person D = new Person("D", 181, "男", 25);
Person E = new Person("E", 166, "女", 19);
Person F = new Person("F", 173, "男", 17);
Person G = new Person("G", 160, "女", 20);
Person H = new Person("H", 174, "男", 21);
Person I = new Person("I", 169, "男", 19);
Person J = new Person("J", 158, "女", 17);
List<Person> list = new ArrayList<Person>();
list.add(A);
list.add(B);
list.add(C);
list.add(D);
list.add(E);
list.add(F);
list.add(G);
list.add(H);
list.add(I);
list.add(J);
Person temp;
for (int k = 0; k < list.size(); k++) {
for (int j = 0; j < list.size() - k - 1; j++) {
if (list.get(j).getHeight() > list.get(j + 1).getHeight()) {
temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
for (Person person : list) {
System.out.println(person);
}
}
}
class Person {
/** 姓名 */
private String name;
/** 身高 */
private int height;
/** 性别 */
private String gender;
/** 年龄 */
private int age;
public Person(String name, int height, String gender, int age) {
this.name = name;
this.height = height;
this.gender = gender;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", height=" + height + ", gender=" + gender + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
运行结果:
展开全部
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class $ {
public static void main(String[] args) {
List<Person> data = new ArrayList<>();
data.add(new Person("a", 166, "男", 18));
data.add(new Person("b", 167, "女", 19));
data.add(new Person("c", 168, "男", 20));
data.add(new Person("d", 169, "女", 17));
data.add(new Person("e", 163, "男", 13));
System.out.println("排序前" + data);
Collections.sort(data, new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return o2.getAge() - o1.getAge();
}
});
System.out.println("排序后" + data);
}
}
class Person {
private String name;
private long shengao;
private String sex;
private int age;
public Person(String name, long shengao, String sex, int age) {
this.name = name;
this.shengao = shengao;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getShengao() {
return shengao;
}
public void setShengao(long shengao) {
this.shengao = shengao;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "{" + name + "," + shengao + "," + sex + "," + age + "}";
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Person{
private String name;
private int height;
private String sex;
private int age;
public Person(String name,int height,String sex,int age){
this.name = name;
this.heigth = height;
this.sex = sex;
this.age = age;
}
public int getHeight(){
return this.height;
}
public String getName(){
return this.name;
}
}
public class ComparatorPerson implements Comparator{
public int compare(Object arg0, Object arg1) {
Person person0 = (Person)arg0;
Person person1 = (person)arg1;
int flag=person0.getHeight().compareTo(person1.getHeight());
return flag;
}
}
public class SortTest {
public static void main(String[] args){
List personlist=new ArrayList();
personlist.add(new Person("a",153,"m",15));
personlist.add(new Person("a",144,"m",15));
personlist.add(new Person("a",171,"m",15));
personlist.add(new Person("a",173,"m",15));
personlist.add(new Person("a",114,"m",15));
personlist.add(new Person("a",153,"m",15));
personlist.add(new Person("a",144,"m",15));
personlist.add(new Person("a",112,"m",15));
personlist.add(new Person("a",178,"m",15));
personlist.add(new Person("a",145,"m",15));
ComparatorUser comparator=new ComparatorUser();
Collections.sort(personlist, comparator);
for (int i=0;i<personlist.size();i++){
Person person_temp=(Person)personlist.get(i);
System.out.println(person_temp.getHeight()+","+person_temp.getName());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询