寒假11:集合体系结构

2022-02-19  本文已影响0人  是归人不是过客
集合类体系结构.png

Collection集合概述和使用
是单例集合的顶层接口,他表示一组对象,这些对象也称为Collection的元素。
JDK不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现。

创建Collection集合对象
1、多态的方式
2、具体的实现类ArrayList

Collection<String> c = new ArrayList<String>();
// 添加元素: boolean add(E e)
c.add("Hello");
c.add("world");

输出:


输出.png

Collection常用方法:


常用方法.png

alt + 7 可以看见所有信息类的结构

Collection 集合的遍历
Iterator:迭代,集合的专用遍历方式
Iterator<E> iterator();返回集合中元素的迭代器,通过集合的iterator()方法得到

Iterator中的常用方法
next(): 返回迭代中的下一个元素
boolean hasNext(): 如果迭代具有更多元素,则返回true

Iterator <String> it = c.iterator();
while(it.hasNext) {
  String s = it.next();
  System.out.println(s);
}

举个例子:


1.png 2.png 3.png
上一篇 下一篇

猜你喜欢

热点阅读