集合类的特点:
提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变
集合的体系结构:
理解xhj:
学习接口的原因是:当学了Collection接口后,再学习List接口,只需要学习List接口特有的内容即可
学习实现类的原因是:接口不能直接创建对象并调用方法,而接口的实现类可以直接创建对象并实现方法调用
位于软件包java.util包下,所以使用的时候需要导包interface Collection < E >,其< E >是泛型,表示集合中元素的类型public interface Collection< E > extends Iterable< E > 表示Collection是一个接口,并且继承自Iterable接口Collection是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素集合层次结构中的根界面;集合表示一组被称为其元素的对象;一些集合允许重复元素(List),而其他集合不允许(Set)JDK不提供此接口的任何直接实现,它提供了更具体的子接口的实现,如Set和List,也就是Collection并没有具体的实现类,要想创建对象必须使用子接口Set和List的实现类创建创建Collection集合的对象:①多态的形式②具体的实现类ArrayList
案例:
public class CollectionDemo {
public static void main(String[] args) {
//创建Collection对象
Collection c = new ArrayList();
//添加元素:boolean add(E e)
c.add("hello");
c.add("java");
//输出collection对象
System.out.println(c);
//这里正常来讲输出的是一串带有包名的字符串,如果要是输出不是此内容,则说明toString方法被重写了。
//输出结果是 [hello, java]
// 说明toString被重写了
}
}
Collection集合常用方法
| 方法名 | 说明 |
|---|---|
| boolean add(E e) | 添加元素 |
| boolean remove(Object o) | 从集合中移除指定的元素 |
| void clear() | 清空集合中的元素 |
| boolean contains (Object o) | 判断集合中是否存在指定的元素 |
| boolean isEmpty() | 判断集合是否为空 |
| int size() | 集合的长度,也就是集合中元素的个数 |
理解xhj:
①Alt + 7 打开一个窗口,能够看到类的所有信息
add
//创建Collection集合对象 Collectionc = new ArrayList (); //boolean add(E e)方法 System.out.println(c.add("hello"));//返回值是true System.out.println(c.add("java"));//返回值是true System.out.println(c.add("java")); System.out.println(c);//输出[hello] [hello, java] ArrayList对象可以添加重复元素, ArrayList对象.add的结果都是true吗? 选中 ArrayList方法,alt + enter按键跟随到ArrayList文件中,找到add方法。快捷方式是:在Idea工具栏上选择View==》Tool Windows==》Structure 即可。
remove
//boolean remove(E e)
System.out.println(c.remove("java"));
//删除的元素存在于集合中,则会被删除,且对象.remove返回值是true
System.out.println(c.remove("javaee"));
// 删除的元素不存在与集合中,则不会删除,且对象.remove返回值是false
System.out.println(c);
clear
c.clear();//清空集合中的所有元素 System.out.println(c);//输出结果是[]
contains
//boolean contains(E e)
System.out.println(c.contains("java"));//true
//要判断的元素在集合中,返回true;不在集合中,返回false
System.out.println(c.contains("javaee"));//false
size
System.out.println(c.size());//2 //输出结果是2,原本是[hello, java, java];删除了一个java,再输出长度就是2了
代码:
public class CollectionDemo {
public static void main(String[] args){
//创建Collection集合对象
Collection c = new ArrayList();
//boolean add(E e)方法
System.out.println(c.add("hello"));//返回值是true
System.out.println(c.add("java"));//返回值是true
System.out.println(c.add("java"));
System.out.println(c);//输出[hello] [hello, java]
//可以添加重复元素,
// ArrayList对象.add的结果都是true吗?
// 选中 ArrayList方法,alt + enter按键跟随到ArrayList文件中,找到add方法。快捷方式是:在Idea工具栏上选择View==》Tool Windows==》Structure 即可。
//boolean remove(E e)
System.out.println(c.remove("java"));
//删除的元素存在于集合中,则会被删除,且对象.remove返回值是true
System.out.println(c.remove("javaee"));
// 删除的元素不存在与集合中,则不会删除,且对象.remove返回值是false
System.out.println(c);
// void clear()
// c.clear();//清空集合中的所有元素 返回值是[]
// System.out.println(c);
//boolean contains(E e)
System.out.println(c.contains("java"));//true
//要判断的元素在集合中,返回true;不在集合中,返回false
System.out.println(c.contains("javaee"));//false
//int size()
System.out.println(c.size());//2
//输出结果是2,原本是[hello, java, java];删除了一个java,再输出长度就是2了
}
}
Collection集合的遍历


