ArrayList array = new ArrayList();
Button b = new Button("a");
array.add(b);
b = new Button("b");
array.add(b);
b = new Button("c");
array.add(b);b = new Button("b");
System.out.println(array.contains(b));
为什么array中不包含b???
Button b = new Button("a");
array.add(b);
b = new Button("b");
array.add(b);
b = new Button("c");
array.add(b);b = new Button("b");
System.out.println(array.contains(b));
为什么array中不包含b???
那什么a,b,c都是Button的属性
就知道了。
return indexOf(elem) >= 0;
} public int indexOf(Object elem) {
if (elem == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}
上面是ArrayList.java里的。
==========================================================
Test.java
import java.util.*;
import java.awt.*;
class Test
{
public static void main(String[] args)
{
ArrayList array = new ArrayList();
Button b = new Button("a");
array.add(b);
b = new Button("b");
array.add(b);
b = new Button("c");
array.add(b);b = new Button("b");System.out.println(array.contains(b));
System.out.println(array);
System.out.println(b);
}
}打印出来的结果
false
[java.awt.Button[button0,0,0,0x0,invalid,label=a], java.awt.Button[button1,0,0,0
x0,invalid,label=b], java.awt.Button[button2,0,0,0x0,invalid,label=c]]
java.awt.Button[button3,0,0,0x0,invalid,label=b]我还是不明白为什么equals的结果不同。
equals用来比较这两个对象是否相同。
但是用在字符串的时候似乎还是有点不一样的地方。
ArrayList array = new ArrayList();
Button b = new Button("a");
array.add(b);
b = new Button("b");
array.add(b);
b = new Button("c");
array.add(b);
System.out.println(array.contains(b));
返回true
就调用了Object.equals()方法,而此方法是比较地址的.
private boolean containBtn(Button btn,List array) {
for (int i = 0,j=array .size();i<j;i++) {
Button button = (Button)array.get(i);
if (btn.getLabel() == null) {
return false;
} else if (button.getLabel().equals(btn.getLabel())) {
return true;
} else{
false;
}
}
}
new Button("a");new Button("b");new Button("c");new Button("b");四个对象,而你只是将前三个对象加进了arraylist中,而b最后指向的哪个对象new Button("b");没有加进去,所以
System.out.println(array.contains(b)); 为false
呵,不知道我的解释能不能让你明白
public class Test {
public static void main(String[] args){
System.out.println(new Button("a").equals(new Button("a")));
}
}
输出false所以说明Button类没有重写equals()方法
看了半天只有wallex(兰舟催发)说的对,还没给分~
VO类
package com.sscm.test;/**
* @author sunny
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class VO {
int id;
String name; VO() { }
VO(int id) {
this.id=id;
} VO(String name) {
this.name=name;
}
/**
* Returns the id.
* @return int
*/
public int getId() {
return id;
} /**
* Returns the name.
* @return String
*/
public String getName() {
return name;
} /**
* Sets the id.
* @param id The id to set
*/
public void setId(int id) {
this.id = id;
} /**
* Sets the name.
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
} public boolean equals(Object obj) {
if (obj instanceof VO) {
VO vo = (VO) obj;
if (vo.name.equals(this.name)){
System.err.println("equals");
return true;
}
}
return false;
}
}测试类
package com.sscm.test;import java.util.ArrayList;/**
* @author sunny
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class Arr {
public static void main(String[] args) {
ArrayList array = new ArrayList();
VO b = new VO("a");
array.add(b);
b= new VO("b");
array.add(b);
b = new VO("c");
array.add(b);
// System.out.println(array.contains(b));
VO bb = new VO("b");
System.out.println("equal "+b.equals(bb)); }}