目录
四、JDK 5.0 新增了foreach循环,用于遍历集合,数组
一、集合框架的概述
1.集合、数组都是对多个数据进行存储操作的结构,简称java容器 说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt .jpg .avi 数据库中) 2.1 数组在存储多个数据方面的特点: >—旦初始化以后,其长度就确定了。 >数组一旦定义好,其元素的类型也就确定了。我们也就只能操作指定类型的数据了。比如: String[] arr;int[] arr1; 2.2 数组在存储多个数据方面的缺点: >—旦初始化以后,其长度就无法修改 >数组中提供的方法非常有限,对于添加、删除、插入数据非常不便,效率不高 >数组中没有提供一个现成的属性或方法来表示存放的元素个数 >数组存放数据的特点:有序、可重复。对于无序不可重复的需求,不能满足
二、集合框架
/----Collection接口:单列集合,用来存储的一个一个的对象 /----List接口:存储有序的,可重复的数据(动态数组) /----ArrayList、LinkedList、Vector /----Set接口:存储的无序的,不可重复的数据(类似于高中的集合) /----HashSet、LinkedHashSet、TreeSet /----Map接口:双列集合,用来存储一对一对的数据。(key、value)(函数) /----HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
三、Collection接口中声明的方法的测试
要求:Collection接口的实现类要重写equals(),判断是否包含某个元素,删除元素,查找,都需要用到 contains(Object obj):判断集合中是否包含这个obj containsAll(Collection coll1):判断形参coll1中的所有元素是否都在当前集合中 remove(Object obj):从当前集合中删除obj元素 removeAll(Collection coll1):差集:从当前集合中删除coll1中的所有元素 retainAll(Collection coll1):交集:保留当前集合和coll1中共有的元素 equals(Collection coll1):判断两个集合的元素是否相等,需要考虑集合是有序的还是无序的 hashCode():返回当前对象的哈希值 toArray():集合--->数组 数组--->集合:调用Arrays类的asList静态方法
public class CollectionTest1 {
@Test
public void test1() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
boolean contains = c.contains(123);
System.out.println(contains);//true
System.out.println(c.contains(new String("haiyu")));//true
System.out.println(c.contains(new Person(12, "haiuyu")));//false
// 总结:contains相当于equals,String中重写过equals,比较的是内容。自定义的类中没有重写过。
// containsAll(Collection coll1):判断形参coll1中的所有元素是否都在当前集合中
Collection coll1 = Arrays.asList(123, 456);
boolean b = c.containsAll(coll1);
System.out.println(b);//true
System.out.println("*************");
c.remove(123);
System.out.println(c);
c.removeAll(coll1);
System.out.println(c);
}
@Test
public void test2() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
Collection coll1 = Arrays.asList(123, 456, 38383, 3);
c.retainAll(coll1);
System.out.println(c);
System.out.println(c.hashCode());//5230
Object[] a = c.toArray();
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
// 数组--->集合:调用Arrays类的asList静态方法
List<String> list = Arrays.asList(new String[]{"aa", "bb", "cc"});//list就是collection
System.out.println(list);
List<int[]> ints = Arrays.asList(new int[]{123, 456});
System.out.println(ints.size());//1
List<Integer> integers = Arrays.asList(new Integer[]{123, 456});
System.out.println(integers.size());//2
}
}
四、JDK 5.0 新增了foreach循环,用于遍历集合,数组
public class ForTest {
@Test
public void test1() {
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12, "haiuyu"));
c.add(false);
//for(集合元素的类型 局部变量:集合对象) for(数组元素的类型 局部变量 : 数组本身)
//内部仍然调用的迭代器
for (Object obj:c){
System.out.println(obj);
}
}
五、使用迭代器Interator接口遍历集合元素
1、内部有两个方法:hasNext() next() 2、内部还有一个remove()方法,可以在遍历的时候,删除集合中的元素,此方法不同于集合直接调用remove()
public class IteratorTest {
@Test
public void test1(){
Collection c = new ArrayList();
c.add(123);
c.add(456);
c.add(new String("haiyu"));
c.add(new Person(12,"haiuyu"));
c.add(false);
Iterator iterator = c.iterator();//可以理解为有了一个指针,指向集合第一个元素之前的一个空的地方
// 方式一:
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next());
// System.out.println(iterator.next()); NoSuchElementException
// 方式二:不推荐
// for (int i = 0; i < c.size(); i++) {
// System.out.println(iterator.next());
// }
// 方式三:推荐
while (iterator.hasNext()){//这里不能是c.iterator.hasNext(),因为每次调用都会是一个新的指针结构出现
System.out.println(iterator.next());
}
}