List集合接口
List集合接口的使用
特点是:有序、有下标、可以重复
1、add、remove、clear
2、list的for的遍历、迭代器Iterator的遍历、迭代器ListIterator的遍历
3、contains判断是否存在、isEmpty判断是否为空、indexOf获取某个元素的位置
## add、remove、clear
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
System.out.println(list.size());//结果为2
System.out.println(list);//结果为[华为、苹果]
list.remove(0);
list.remove("华为");
System.out.println(list);//结果为苹果
list.clear();
System.out.println(list.size());//结果为0
```
## for的遍历、Iterator的遍历、ListIterator的遍历
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}//结果为[华为、苹果]
```
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
for (Object object : list){
System.out.println(object);
}//结果为[华为、苹果]
```
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
Iterator n = list.iterator();
while (n.hasNext()){
System.out.println(n.next());
}//结果为[华为、苹果]
```
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
ListIterator m = list.listIterator();
while (m.hasNext()){
System.out.println(m.nextIndex()+":"+m.next());
}//结果为0:华为
// 1:苹果
```
## contains、isEmpty、indexOf
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
System.out.println(list.contains("华为"));//结果为true
System.out.println(list.isEmpty());//结果为flase
```
```java
List list = new ArrayList();
list.add("苹果");
list.add(0,"华为");
System.out.println(list.indexOf("华为"));//结果为0
```
注意数值!!remove时要进行拆箱!!
```java
List list = new ArrayList();
//添加数值(这里自动装箱了!!!)
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println("元素个数:"+list.size());//结果为元素个数:5
System.out.println(list);//结果为[20, 30, 40, 50, 60]
list.remove((Object) 20);//上面自动装箱了要转换类型进行拆箱!!!!
System.out.println(list.size());//结果为4
//方法subList,返回子集合!含头不含尾
List subList = list.subList(1,3);
System.out.println(subList.toString());//结果为[40, 50]
```