中易网

JAVA中,怎么比较多个对象的大小?

答案:2  悬赏:80  
解决时间 2021-03-12 01:37
比如都按照年龄比较。记住,是多个对象,不是两个对象比较大小。希望各位高手给个具体示例。谢谢。
最佳答案
实现Comparable接口,然后把所有的对象加到一个ArrayList中,然后用Collections.sort排序

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Compare {
public static void main(String[] args) {
Student stu1 = new Student("stu1",10,150);
Student stu2 = new Student("stu2",9,140);
Student stu3 = new Student("stu3",10,160);
Student stu4 = new Student("stu4",10,140);
Student stu5 = new Student("stu5",11,152);
Student stu6 = new Student("stu6",12,151);
Student stu7 = new Student("stu7",8,140);
Student stu8 = new Student("stu8",10,156);
Student stu9 = new Student("stu9",11,149);
Student stu10 = new Student("stu10",10,155);
Student stu11 = new Student("stu11",13,152);
List list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
list.add(stu6);
list.add(stu7);
list.add(stu8);
list.add(stu9);
list.add(stu10);
list.add(stu11);

//比较大小
Collections.sort(list);
for(Student stu : list) {
System.out.println(stu);
}
}

}
class Student implements Comparable {
private int age;
private int height;//身高
private String name;
public Student(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getHeight() {
return height;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int compareTo(Student o) {
//先按年龄比较,再按身高比较
if(this.getAge() > o.getAge()) {
return 1;
} else if(this.getAge() == o.getAge()) {
if(this.getHeight() > o.getHeight()) {
return 1;
} else if(this.getHeight() == o.getHeight()) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}

@Override
public String toString() {
return "Name:" + this.getName() + ";age:" + this.getAge() + ";height:" + this.getHeight();
}
}
全部回答
object流 直接将object写入或读出 也叫做对象的序列化:把一个object直接转换成字节流写到硬盘上,或者直接写到网络上去 这里有一个类t: class t implements serializable { // 注意,必须要实现serializable接口,才是可序列化的 int i = 10; int j = 9; double d = 2.3; int k = 15; } 这个类里面的成员变量3个int,1个double,int是4字节,double是8字节 所以对象的大小绝对不会小于20字节,但是t肯定是从object类继承,那么继承下来这些内容也 占空间,比如方法什么的,所以肯定要大于20字节了 运行下面这个类: import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable; public class test { public static void main(string[] args) { t t = new t(); try { fileoutputstream fos = new fileoutputstream("d:/abc.txt"); objectoutputstream oos = new objectoutputstream(fos); oos.writeobject(t); oos.flush(); oos.close(); } catch (ioexception e) { e.printstacktrace(); } } } 直接将t对象通过object流写到了d盘的名为abc.txt的文件里面 一个空文本文件是0字节,找到这个文件以后查看它的属性,我这里是67 字节, 那么这个对象就是67 字节 现在在t类中添加一些其他内容,那么再次输出,字节数肯定就比原来大了,它就是把整个对象转换成 字节,写入了文件,这个应该算是比较好的方式了,但是我们无法特别精确的知道对象中哪些内容各自 占用多少空间,因为那是java虚拟机的事了,我学的没那么深入,水平有限 当然,把一个对象写进去,也可以读出来: fileinputstream fis = new fileinputstream(""); objectinputstream ois = new objectinputstream(fis); // 当成object读出来的,强转成t类型 t treaded = (t)ois.readobject(); system.out.println(treaded.i + " " + treaded.j + " " + treaded.d + " " + treaded.k); 发现打印出的成员变量的值,还是存进去这个对象的值
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
斜桥地址有知道的么?有点事想过去
找一部外国的科幻片只记得开始是男主角一个人
武进鸿程汽车配件公司地址有知道的么?有点事
总把新桃换旧符的诗句它的前一句是什么
多项选择题。法的规范性表现在()。 请问正
学中餐有哪些专业
李宁逐风超轻透气跑鞋适合哪个季节穿?
北京八中现任老师名单
IFS国际金融中心2号门我想知道这个在什么地方
日语"改变"怎么说
请问我拉尿的时候尿滴了一些到裤子上然后又坐
DHC适合18岁女生用吗
开机出现磐正主板啥意思
一品香石锅鱼我想知道这个在什么地方
LG G5有无线充电吗
推荐资讯
氧化石墨烯在100℃水溶液下会还原吗
9.75g的锌能与多少摩尔的Hcl刚好完全反应?求
新蜀门怎么挂机
真丝和纱料在一起会起静电吗
尺码有 S .M.L.XL我身高一米六16岁该买多大裤
女朋友主动给你说让你去开房,我开好房之后,
我是11岁半女生,胸部一点也没发育,身高142c
怎么调换两个微信绑定的手机号码
武汉话版《大话西游》中的经典对白
不高兴的津津 c++题,做好更多大大的赏!!!
下雪天路滑,不小心滑到了,屁股着地,摔倒了
少年西游记 法宝精华有必要买吗
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?