刪除集合元素Collection ,remove()

package seday11;
/**
* @author xingsir
*/
public class coordinate {java

private int x;
private int y;
/*
* 右鍵點-Source-點 -generate constructor using fields,選擇要生成的屬性
* 這個選項自動生成帶參數的 構造函數
*/
public coordinate(int x, int y) {
super();
this.x = x;
this.y = y;
}
/*
* 右鍵點-Source-點 -generate getters and setters,選擇要生成的屬性
*/
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
return"("+x+","+y+")";
}函數

/*
* 右鍵點-Source-點 -generate hashCode() and equals(Object obj),選擇要生成的屬性
* 這個選項自動生成
*/
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
// 右鍵點-Source-點 -
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
coordinate other = (coordinate) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}this

}rem

//======================================================================get

package seday11;hash

import java.util.ArrayList;
import java.util.Collection;io

/**
* @author xingsir
* 刪除集合元素
* boolean remove()從集合中刪除給定元素,刪除的是集合中與給定元素equals比較爲true的元素。
*/
public class CollectionDemo2 {class

public static void main(String[] args) {
Collection c= new ArrayList();
c.add(new coordinate(1, 1));
c.add(new coordinate(2, 2));
c.add(new coordinate(3, 3));
c.add(new coordinate(4, 4));
c.add(new coordinate(5, 5));
System.out.println(c);

coordinate p =new coordinate(5,5);
c.remove(p);
System.out.println(c);


}import

}sed

相關文章
相關標籤/搜索