/*
集合框架的概述
1.集合、数组都是对多个数组进行存储操作的,坚持java容器
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(txt.jpg.avi,数据库中存储的数据)
2.数组在存储多个数据房勉的特点:
》一旦初始化以后,其长度就确定了
》数组一旦定义好,其元素类型就确定了,只能操作指定类型的数据,比如说:String[] arr;int[] arr1
2.2数组存储多个数据方面的缺点
》一旦初始化以后,其长度不可修改
》数组中提供的方法有限,对于添加。删除。插入数据等操作,非常不便
》获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用
》数组存储数据的特点:有序、可重复、对于无序、不可重复的需求,不能满足。
二、集合框架
|---Collection接口
|---List接口:存储有序的,可重复的数组---->"动态“数组
|----ArrayList/LinkedList/Vector
|---Set接口:存储无序的、不可重复的数据 ---》高中集合问题
|---HashSet/LinedHashSet/TreeSet
|----Map接口:双列集合,用来存储一对(key - value)一对的数据
\---HashMap/LinkedHashMap/TreeMap/Hashtable/Properties
三、Collection接口中方法的使用
*/
public class CollectionTest {
@Test
public void test() {
Collection coll = new ArrayList();
coll.add(12);
coll.add("AA");
coll.add("BB");
coll.add(new Date());
// clear():清空集合元素
// size():获取添加元素的个数
System.out.println(coll.size());
// isEmpty():判断当前集合是否为空
}
@Test
public void test1() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add("AAA");
coll.add(false);
Person p = new Person("Jerry", 20);
coll.add(p);
coll.add(new Person("Jerry", 20));
// 1. contains(Object obj):判断当前集合中是否包含obj;
boolean contains = coll.contains(123);
System.out.println(contains);
System.out.println(coll.contains(new String("Tom")));
System.out.println(coll.contains(p));
System.out.println(coll.contains(new Person("Jerry", 20)));
//2.containsAll(Object obj):判断当前coll1中所有元素是否都存在于当前集合中
Collection coll1 = Arrays.asList(123, 456);
System.out.println(coll.containsAll(coll1));
}
@Test
public void test2() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add("AAA");
coll.add(false);
Person p = new Person("Jerry", 20);
coll.add(p);
coll.remove(1234);
System.out.println(coll);
System.out.println(coll.size());
// removeAll(Collection coll):从当前集合中移除coll1中的所有元素
Collection coll1 = Arrays.asList(123, 456);
coll.removeAll(coll1);
System.out.println(coll);
}
@Test
public void test3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new String("Tom"));
coll.add(new Person("Jerry", 20));
coll.add("AAA");
coll.add(false);
// Person p = new Person("Jerry", 20);
// coll.add(p);
// 5 retainAll(Collection coll1):获取当前集合和coll集合的交集,并返回给当前集合
// Collection coll1 = Arrays.asList(123,456,789);
// coll.retainAll(coll1);
// System.out.println(coll);
//6. equals(Object obj):要想返回true需要当前集合和形参集合相同
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add(456);
coll1.add(new String("Tom"));
coll1.add(new Person("Jerry", 20));
coll1.add("AAA");
coll1.add(false);
// Person e = new Person("Jerry", 20);
// coll.add(e);
System.out.println(coll.equals(coll1));
// 7. hashCode();返回当前对象的哈希值
System.out.println(coll.hashCode());
// 8. toArray():返回当前对象的数组
Object[] arr = coll.toArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
// 数组---》集合:调用ArrayList的静态方法asList()
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(list);
}
//jdk5.0新增了foreach循环,用于遍历集合,数组
public class ForTest {
@Test
public void test1() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry", 20));
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
// for(结合元素的类型 局部变量:集合对象)
for (Object obj : coll) {
System.out.println(obj);
}
}
@Test
public void test2() {
int[] arr = new int[]{1, 2, 3, 4, 5, 6};
for (int i : arr) {
System.out.println(i);
}
}
@Test
public void test3() {
String[] arr = new String[]{"mm", "mm", "mm"};
// 普通for循环
// for (int i = 0; i < arr.length; i++) {
// arr[i] = "GG";
// }
// 增强for循环
for (String s : arr) {
s = "GG";
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
//使用集合元素的遍历操作:使用迭代器Iterator接口
/*
1.内部方法:hashNext()和next()
2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象
默认游标都在集合的的第一个元素之前
3.内部定义了remove(),可以再遍历的时候删除集合中的元素。此方法不同于集合直接调用()
*/
public class IteratorTest {
@Test
public void test1() {
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry", 20));
coll.add(new String("Tom"));
coll.add(false);
Iterator iterator = coll.iterator();
// 方式二:不推荐
// for (int i = 0; i <coll.size() ; i++) {
// System.out.println(iterator.next());
// }
// 方式三:推荐
while (iterator.hasNext()) {
Object obj = iterator.next();
if("Tom".equals(obj)){
coll.remove(obj);
}
}
iterator = coll.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
}