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.
分享到:
相关推荐
"在线考试系统代码001"是一个基于ASP(Active Server Pages)和Access数据库开发的在线考试平台。ASP是一种微软公司的服务器端脚本语言,常用于构建动态网页,而Access则是一款关系型数据库管理系统,适用于小型项目...
vb源代码001YUSGABUCSBUBZJKIHNINXIBNIDABC怒ishnfibnci可不能 inbi不你说比男性健康才能比上半年纳斯卡能彼此inikbnlsi 你i 你这里拿出 很可能才比我好崇拜
智能小车竞速代码主要涉及的是自动化控制、嵌入式系统和机器人技术的融合应用。在这一领域,小车能够自主导航,避开障碍,并在赛道上进行高速行驶,这背后离不开一系列复杂的技术支撑。 首先,我们要理解“寻迹功能...
课程难度:中 课程目标:学习云开发,云开发后台管理,学习nodejs ...如果掌握了该门课程,那么相信你对小程序云开发已非常了解,即便你依葫芦画瓢也可以独自完成很多其他产品1.该门课程不属于基础课程,在学习前,希望...
按键去抖动关键在于提取稳定的低电平状态,滤除前沿,后沿抖动毛刺。对于一个按键信号,可以用一个脉冲对它进行采样。如果连续三次采样为低电平,可以认为信号已经处于稳定状态,这是输出一个低电平按键信号。...
隐私防火墙测试001,AKLT3.0键盘测试.rar
《全面解析:chunghop万能遥控器Q-001操作手册与代码表》 在现代家庭中,空调已经成为不可或缺的电器之一,而拥有一款高效的万能遥控器则能让空调操控更加便捷。chunghop万能遥控器Q-001便是其中的佼佼者,其强大的...
【SPL06-001驱动代码】是专为STC32G和STC8H系列微控制器设计的一款气压传感器驱动程序。这款驱动主要用于配合SPL06-001气压传感器,该传感器能精确测量环境中的大气压力,广泛应用于气象监测、物联网设备、户外运动...
采集代码 更能齐全简单易用
【SPL06-001代码】是一个针对MINI无人机和DIY大四轴项目的驱动程序代码,这个代码库提供了必要的控制逻辑和接口,使得这些小型飞行设备能够有效地运行和控制。SPL06-001标签是这个特定代码的标识符,可能代表一种...
而代码001则是指示重新初始化运行,通常在断电或异常后需要执行这一操作以恢复正常功能。当遇到代码006时,表明GDCB的RAM存储记录失败,这可能会影响到电梯的控制逻辑。技术人员需检查GDCB及其相关联的存储单元,以...
由中国卫生信息与健康医疗大数据学会(原中国卫生信息学会)批准发布的《T/CHIA 001-2017手术、操作分类与代码》团体标准于近日发布,于2018年1月1日起正式实施。 我国自上世纪90年代以来将ICD-9-CM-3(国际疾病...
失败结果的代码包括M001、M002等多种代码,每种代码对应不同的错误信息。 * 代码M001的解释:当固件升级失败,且错误信息是免费号码M1时,升级结果将返回M001代码。该代码表明固件升级失败,需要重新尝试升级。 * ...
"竹林溪径CY001工程代码"是一个特定的编程项目,可能是一个软件或应用程序的源代码集合。从描述中我们可以推断,这个代码库原本分散在不同的文件夹中,这给整体编译和运行带来了困难。为了方便用户直接下载和编译,...
举例来说,一款汽车用的组合仪表,如果它的转速表有多个变型设计,可以通过产品属性代码90,产品设计顺序号ZB001,变型设计代码001,部件代码001来唯一标识转速表的机芯。 总结,产品编码规则为企业提供了一套规范...
MQ 错误代码表 MQ 错误代码表是 IBM WebSphere MQ 中用于描述错误的代码表。这些代码用于 MQ 客户端和服务器之间的通信中,用于描述错误的原因和解决方法。 MQRC_NONE (0, X'000"):没有理由的报告。如果是 MQCC_...
小游戏设计的全部代码,下载就可以正常运行
- 注塑(代码001) - 真空电镀(代码002) - 喷涂(代码003) - 印刷(代码004) - 烫金(代码005) - 镭雕(代码006) #### 七、原材料编码规则 原材料编码规则尚未在提供的文档中完全展示,但可以推测其结构与...
2. **系统速拨号码设定**(代码001):可以设置00-99共100个速拨号码,方便用户快速拨打常用电话。 3. **分机号码设定**(代码003):设定分机的号码。 4. **分机名称设定**(代码004):为每个分机设定名称,便于...