一、概述
Java单列集合中最顶层的是Collection接口,Collection下又分List和Set两大类:

学习Java集合的时候,建议自顶向下学:先学Collection,再学List和Set,最后学各种具体的集合。
二、Collection接口

1、Collection接口继承了Iterable接口,于是就有了Iterable的一些特性:
a、具有迭代器方法:
b、能够使用增强for循环
示例:
1 | public static void main(String[] args) { |
2、Collection的常用方法

示例:
1 | a、代码示例 |
注意:Collection的子类继承了Collection的方法,即List和List的后代、Set和Set的后代,都可以使用上面这些方法!
三、List接口

List的特有方法:

1、
1 | a、代码示例 |
2、
1 | List<Integer> list=new ArrayList<>(); |
结果:3
3、
1 | List<Integer> list=new ArrayList<>(); |
结果:
3
666
4、
1 | List<Integer> list=new ArrayList<>(); |
结果:[3, 666, 1, 4, 1, 5]
5、

结果:
5
[3, 1, 4, 1]
false
[3, 1, 4, 1]
6、7:
1 | List<Integer> list=new ArrayList<>(); |
结果:
0
5
-1
8、9:
1 | List<Integer> list=new ArrayList<>(); |
结果:
0
1
2
1 | List<Integer> list=new ArrayList<>(); |
结果:
1
2
10、
1 | List<Integer> list=new ArrayList<>(); |
结果:[1, 2]
四、Set接口
Set中的方法如下:

从上图可知,Set中都是它老子Collection的方法!真是个啃老族啊!
由于篇幅太长,有关ArrayList、LinkedList、Vector、HashMap、LinkedHashMap、TreeSet的内容将在下一篇博客进行介绍!
(其实把父类的方法掌握了基本就够用了)
最后附上一个斗地主小游戏:
1 | import java.util.ArrayList; |
运行结果:
