java.util 包中提供了一些集合类,这些集合类又被称为容器。
提到容器就会想到数组,集合类与数组的不同之处在于:数组的长度是固定的,集合的长度是可变的;数组用来存放基本类型的数据,集合用来存放对象的引用。常用的集合有List、Set、Map集合。
collection是一个接口,定义了集合相关的操作方法,其有两个子接口:List与Set。
List:可重复集
Set:不可重复集
元素是否重复取决于元素的equals的比较结果。
集合中存储的都是引用类型元素,并且集合只保存每个元素对象的引用,而并非将元素对象本身存入集合。
Collection接口是层次中的跟接口。构成Collection的单位成为元素。Collection接口通常不能直接使用。
Collection接口常用方法:
add(E e) //将指定的对象添加到该集合中
remove(Object o) //将指定的对象从该集合中移除
isEmpty() //返回布尔值,用于判断当前集合是否为空
iterator() //返回在此Collection的元素上进行迭代的迭代器,用于遍历集合中的对象
size() //返回int值,获取该集合中元素的个数
contains(Object o) //返回布尔值,用于判断给定元素是否被包含在集合中
注意:集合在判断元素是否被包含在集合中是根据对每个元素用equals方法进行比较后的结果。通常有必要重写equals()保证contains()方法的合理结果。
void clear() //用于清空当前集合
package mycollection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* 测试Collection
*
* @author unique_19
*
*/
public class TestCollection {
public static void main(String[] args) {
testRef();
System.out.println("*********************************************");
testAdd();
System.out.println("*********************************************");
testContains();
System.out.println("*********************************************");
testSizeAndClearAndIsEmpty();
}
/**测试集合持有对象**/
public static void testRef(){
Collection<Cell> cells = new ArrayList<Cell>();//实例化集合对象
cells.add(new Cell(1,2));//向集合里添加元素(1,2)
Cell cell = new Cell(2,3);
cells.add(cell); //向集合里添加元素(2,3)
System.out.println(cell);
System.out.println(cells);
cell.drop();//cell引用所指对象(2,3)变为(3,3)
System.out.println(cell);
System.out.println(cells);//集合内的引用未变,是引用所指向的对象内容发生改变
}
/**测试add方法**/
public static void testAdd(){
Collection<String> c = new ArrayList<String>();
System.out.println(c);//集合为空
c.add("a");
c.add("b");
c.add("c");
System.out.println(c);
Iterator<String> it = c.iterator();//创建迭代器
while (it.hasNext()){//判断是否有下一个元素
String str = it.next();//获取集合中的元素
System.out.println(str);
}
}
/**用contains方法测试集合类中是否包含某个元素**/
public static void testContains(){
Collection<Cell> cells = new ArrayList<Cell>();
cells.add(new Cell(1,2));
cells.add(new Cell(1,3));
cells.add(new Cell(2,2));
cells.add(new Cell(1,3));
Cell cell = new Cell(1,3);
//***List集合的contains方法和对象的equals方法相关联***/
boolean flag = cells.contains(cell);
//***如果Cell类里不重写equals方法将返回false***/
System.out.println(flag);
}
/**测试size 、clear、isEmpty的用法**/
public static void testSizeAndClearAndIsEmpty(){
Collection<String> c = new HashSet<String>();
System.out.println(c.isEmpty());
c.add("java");
c.add("cpp");
c.add("php");
c.add("c#");
c.add("objective-c");
System.out.println("isEmpty:"+c.isEmpty()+",size:"+c.size());
c.clear();
System.out.println("isEmpty:"+c.isEmpty()+",size:"+c.size());
}
}
package mycollection;
public class Cell {
int row;
int col;
public Cell(int row,int col){
this.row = row;
this.col = col;
}
public Cell(){
this(0,0); //调用自己一个有参数的构造方法
}
public Cell(Cell cell){
this(cell.row,cell.col);
}
public void drop(){
row++;
}
public void moveRight(){
col++;
}
public void moveLift(){
col--;
}
@Override
public String toString(){
return "("+row+","+col+")";
}
@Override
public boolean equals(Object o){
if(o==null){ //对象为空
return false;
}
if(this==o){ //对象相同
return true;
}
if(o instanceof Cell){ //判断该对象是否是Cell的实例
Cell cell = (Cell)o;
return cell.row==row && cell.col==col; //判断对象内容是否相同
}else {
return false;
}
}
}