`
trix
  • 浏览: 82345 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

代码001

 
阅读更多
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.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics