package seday11;
/**
* @author xingsir
*/
public class coordinate {
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;
}
}
//======================================================================
package seday11;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author xingsir
* 删除集合元素
* boolean remove()从集合中删除给定元素,删除的是集合中与给定元素equals比较为true的元素。
*/
public class CollectionDemo2 {
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);
}
}
//=========================================================================================
package seday11;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author xingsir
* 集合只能存放引用类型元素,而且保存的也是元素的引用(地址)
*/
public class CollectionDemo4 {
public static void main(String[] args) {
Collection c=new ArrayList();
coordinate p=new coordinate(3, 3);
c.add(p);//添加
/*执行结果:
p:(3,3)
c:[(3,3)]
p:(4,3)
c:[(4,3)]
*/
System.out.println("p:"+p);
System.out.println("c:"+c);
p.setX(4);//设置x为4
System.out.println("p:"+p);
System.out.println("c:"+c);
}
}