首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

技术丶交流 java对象数组的概述和使用(2)

技术丶交流 java对象数组的概述和使用(2)

15.04 集合的继承体系图解集合容器因为内部的数据结构不同,有多种具体容器,根据共性内容不断的向上抽取,就形成了集合框架。
框架的顶层Collection接口
15.05 Collection集合的功能概述Collection 层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
15.06 Collection集合的基本功能测试成员方法:1.  boolean add(Ee):确保此 collection 包含指定的元素(可选操作)。2.  boolean remove(Objecto):从此 collection 中移除指定元素的单个实例,如果存在的话(可选操作)。
3.  void clear():移除此 collection 中的所有元素(可选操作)。4.  boolean contains(Objecto):如果此 collection 包含指定的元素,则返回 true。
5.  boolean isEmpty():如果此 collection 不包含元素,则返回 true。6.  int size():返回此 collection 中的元素数。例: 1 // 创建集合对象 2 // Collection c = new Collection(); //错误,因为接口不能实例化 3 Collection c = new ArrayList(); 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 // c.clear();//移除所有元素 8 // System.out.println("remove:" + c.remove("hello"));//移除一个元素 9 // System.out.println("remove:" + c.remove("javaee"));10 // 判断集合中是否包含指定的元素11  System.out.println("contains:"+c.contains("hello"));//contains:true12  System.out.println("contains:"+c.contains("android"));//contains:false13  //判断集合是否为空14  System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false15 //元素的个数16 System.out.println("size:"+c.size());//size:317 System.out.println("c:" + c);//c:[hello, world, java]15.07 Collection集合的高级功能测试成员方法:1.  boolean addAll(Collection<? extends E> c):将指定 collection 中的所有元素都添加到此 collection 中(可选操作)。2.  boolean removeAll(Collection<?> c):移除此 collection 中那些也包含在指定 collection 中的所有元素(可选操作)。
3.  boolean containsAll(Collection<?> c):如果此 collection 包含指定 collection 中的所有元素,则返回 true。4.  boolean retainAll(Collection<?> c):仅保留此 collection 中那些也包含在指定 collection 的元素(可选操作)。换句话说,移除此 collection 中未包含在指定 collection 中的所有元素。
例:c1.addAll(c2);//将c2集合中的所有元素添加到c1集合中,c1变c2不变c1.removeAll(c2);//将c1集合中与c2集合相同的所有元素删除,只要有一个相同的就返回truec1.containsAll(c2);//判断c1集合中的元素是否包含c2中的全部元素,全部包含则返回truec1.retainAll(c2);//将c1集合中与c2集合相同的元素保留,删除其他元素,返回值表示c1集合是否发生变化,发生变化返回true,没有变化返回false15.08 集合的遍历之集合转数组遍历Object[] toArray():返回包含此 collection 中所有元素的数组。例: 1 public class Practice 2 { 3     public static void main(String[] args) 4     { 5         // 创建集合 6         Collection c = new ArrayList(); 7         c.add("hello"); 8         c.add("world"); 9         c.add("java");10         11         Object[] objs = c.toArray();12         for (int i = 0; i < objs.length; i++) 13         {14             //向下转为String类型15             String s = (String)objs;16             System.out.println(s+":"+s.length());17         }18     }19 }运行结果:hello:5world:5java:415.09 Collection存储自定义对象并遍历案例(使用数组)例: 1 public class Practice 2 { 3     public static void main(String[] args) 4     { 5         // 创建集合 6         Collection c = new ArrayList(); 7         //创建学生对象并添加到集合 8         c.add(new Student("小明",23)); 9         c.add(new Student("小红",32));10         c.add(new Student("小强",14));11         c.add(new Student("旺财",8));12         c.add(new Student("张三",16));13         14         Object[] objs = c.toArray();15         for (int i = 0; i < objs.length; i++) 16         {17             Student s = (Student)objs;18             System.out.println(s.getName()+":"+s.getAge());19         }20     }21 }运行结果:小明:23小红:32小强:14旺财:8张三:1615.10 集合的遍历之迭代器遍历Iterator<E> iterator():返回在此 collection 的元素上进行迭代的迭代器。例: 1 // 创建集合 2 Collection c = new ArrayList(); 3 //创建元素并添加到集合 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 //获取迭代器,实际返回的是子类对象,多态 8 Iterator it = c.iterator(); 9 while(it.hasNext())10 {11     System.out.println(it.next());12 }
继承事业,薪火相传
返回列表