public class EqualsTest {
public static void main (String [] args) {
Moof one = new Moof(8);
Moof two = new Moof(8);
if (one.equals(two)) {
System.out.println("one and two are equal");
}
}
}
class Moof {
private int moofValue;
Moof(int val) {
moofValue = val;
}
public int getMoofValue() {
return moofValue;
}
public boolean equals(Object o) {
if ((o instanceof Moof) && (((Moof)o).getMoofValue()
== this.moofValue)) {
return true;
} else {
return false;
}
}
}
The equals() Contract
Pulled straight from the Java docs, the equals() contract says
■ It is reflexive. For any reference value x, x.equals(x) should return true.
■ It is symmetric. For any reference values x and y, x.equals(y) should
return true if and only if y.equals(x) returns true.
■ It is transitive. For any reference values x, y, and z, if x.equals(y) returns
true and y.equals(z) returns true, then x.equals(z) must return true.
■ It is consistent. For any reference values x and y, multiple invocations of
x.equals(y) consistently return true or consistently return false, provided
no information used in equals comparisons on the object is modified.
■ For any non-null reference value x, x.equals(null) should return false.
List myList = new ArrayList();
As of Java 5 you'll want to say
List<String> myList = new ArrayList<String>();
This kind of declaration follows the object oriented programming principle of
"coding to an interface", and it makes use of generics.
List<String> test = new ArrayList<String>();
String s = "hi";
test.add("string");
test.add(s);
test.add(s+s);
System.out.println(test.size());
System.out.println(test.contains(42));
System.out.println(test.contains("hihi"));
test.remove("hi");
System.out.println(test.size());
which produces
3
false
true
2
There's lots going on in this small program. Notice that when we declared the
ArrayList we didn't give it a size. Then we were able to ask the ArrayList for
its size, we were able to ask it whether it contained specific objects, we removed an
object right out from the middle of it, and then we rechecked its size.
List myInts = new ArrayList(); // pre Java 5 declaration
myInts.add(new Integer(42)); // had to wrap an int
As of Java 5 we can say
myInts.add(42); // autoboxing handles it!
In this last example, we are still adding an Integer object to myInts (not an int
primitive); it's just that autoboxing handles the wrapping for us.
Of course, ArrayList doesn't give you any way to sort its contents,
but the java.util.Collections class does
import java.util.*;
class TestSort1 {
public static void main(String[] args) {
ArrayList<String> stuff = new ArrayList<String>(); // #1
stuff.add("Denver");
stuff.add("Boulder");
stuff.add("Vail");
stuff.add("Aspen");
stuff.add("Telluride");
System.out.println("unsorted " + stuff);
Collections.sort(stuff); // #2
System.out.println("sorted " + stuff);
}
}
This produces something like this:
unsorted [Denver, Boulder, Vail, Aspen, Telluride]
sorted [Aspen, Boulder, Denver, Telluride, Vail]
class DVDInfo implements Comparable<DVDInfo> { // #1
// existing code
public int compareTo(DVDInfo d) {
return title.compareTo(d.getTitle()); // #2
} }
In line 1 we declare that class DVDInfo implements Comparable in such a way
that DVDInfo objects can be compared to other DVDInfo objects. In line 2 we
implement compareTo() by comparing the two DVDInfo object's titles. Since we
know that the titles are Strings, and that String implements Comparable, this is an
easy way to sort our DVDInfo objects, by title. Before generics came along in Java 5,
you would have had to implement Comparable something like this:
class DVDInfo implements Comparable {
// existing code
public int compareTo(Object o) { // takes an Object rather
// than a specific type
DVDInfo d = (DVDInfo)o;
return title.compareTo(d.getTitle());
} }
It’s important to remember that when you override equals() you MUST
take an argument of type Object, but that when you override compareTo() you
should take an argument of the type you’re sorting.
分享到:
相关推荐
vb源代码001YUSGABUCSBUBZJKIHNINXIBNIDABC怒ishnfibnci可不能 inbi不你说比男性健康才能比上半年纳斯卡能彼此inikbnlsi 你i 你这里拿出 很可能才比我好崇拜
一个不错的在线考试系统,运用asp+access编写的,
课程难度:中 课程目标:学习云开发,云开发后台管理,学习nodejs ...如果掌握了该门课程,那么相信你对小程序云开发已非常了解,即便你依葫芦画瓢也可以独自完成很多其他产品1.该门课程不属于基础课程,在学习前,希望...
按键去抖动关键在于提取稳定的低电平状态,滤除前沿,后沿抖动毛刺。对于一个按键信号,可以用一个脉冲对它进行采样。如果连续三次采样为低电平,可以认为信号已经处于稳定状态,这是输出一个低电平按键信号。...
隐私防火墙测试001,AKLT3.0键盘测试.rar
采集代码 更能齐全简单易用
chunghop万能遥控器Q-001说明书及代码表
001消防工程安装的通用.doc
由中国卫生信息与健康医疗大数据学会(原中国卫生信息学会)批准发布的《T/CHIA 001-2017手术、操作分类与代码》团体标准于近日发布,于2018年1月1日起正式实施。 我国自上世纪90年代以来将ICD-9-CM-3(国际疾病...
spl06-001适用stc32g,stc8h,接入iic可以直接使用
考试管理网站系统源代码--001考试管理网站系统源代码--001考试管理网站系统源代码--001
SPL06-001驱动代码,可以直接使用,主要用在MINI无人机和DIY大四轴上
MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表MQ错误代码表
小游戏设计的全部代码,下载就可以正常运行
COMSSensor操作C源代码MT9T001 COMSSensor操作C源代码MT9T001
3、压板墨垫清零(如果出现报错:比如 错误 代码 001,没关系的,因为压板墨垫属损耗材料, 不用理它,只要没真的坏,就不用更换)。 4、墨水计数器设置: 墨垫 MAIN 废墨值 0 点 执行 联系电话:15361618884
VBnet001-勾月农历日历表源代码、可显示星期、属性,具有鼠标拖动,右键等功能。
易语言源代码_测试001.zip
ARM_cortex_m3书源代码.part001.rar ,由于太大太多,所以有3个分卷
自动采集小说书库网站源代码--001自动采集小说书库网站源代码--001 http://bbs.fytcw.com http://bbs.fytcw.com