JAVA中,怎么比较多个对象的大小?
答案:2 悬赏:80
解决时间 2021-03-12 01:37
- 提问者网友:我喜歡係
- 2021-03-11 15:23
比如都按照年龄比较。记住,是多个对象,不是两个对象比较大小。希望各位高手给个具体示例。谢谢。
最佳答案
- 二级知识专家网友:心与口不同
- 2021-03-11 15:47
实现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();
}
}
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.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();
}
}
全部回答
- 1楼网友:气场征服一切
- 2021-03-11 15:57
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); 发现打印出的成员变量的值,还是存进去这个对象的值
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯