List接口(Collection子接口)

List接口基本介绍

首先明确,List接口是Collection接口的子接口

  • List集合类中元素有序,与其加入的顺序一致,并且里面的内容可以重复。
  • List集合中每一个元素都有器对应的顺序索引,即支持索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
public static void main(String [] args){

Dog dog1=new Dog("ahhua",12);
Dog dog2=new Dog("a",15);
Dog dog3=new Dog("ah",13);
Dog dog4=new Dog("ah",13);
//迭代器遍历
List list=new ArrayList();
list.add(dog1);
list.add(dog2);
list.add(dog3);
list.add(dog4);
//支持索引
System.out.println(list.get(2));
for (Object a:list){
System.out.println(a);
}

}
}
class Dog{
private String name;
private int age;

public Dog(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

image-20220704180534499List接口常用的方法

除了我们前面用到的方法

  • 插入一个对象, add方法
  • 返回元素所在的第一个位置/最后的位置 indexOf/lastIndexOf
  • 移除指定index位置的元素,并且返回此元素 remove
  • 设定指定位置为某个元素 set
  • 返回某个范围的子集 sublist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
public static void main(String [] args){

Dog dog1=new Dog("ahhua",12);
Dog dog2=new Dog("a",15);
Dog dog3=new Dog("ah",13);
Dog dog4=new Dog("ah",13);
//迭代器遍历
List list=new ArrayList();
list.add(dog2);
list.add(dog1);
list.add(dog2);
list.add(dog3);
//dog4插入到前面
list.add(0,dog4);
//返回位置
System.out.println(list.indexOf(dog2));
System.out.println(list.lastIndexOf(dog2));
//移除指定的元素,并且返回
System.out.println(list.remove(2).toString());
//指定位置为指定元素
list.set(1,dog4);
System.out.println(list.subList(0,2));

}
}
class Dog{
private String name;
private int age;

public Dog(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

image-20220704180643800List遍历方式

与Collection遍历相同

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class Test {
public static void main(String [] args){

Dog dog1=new Dog("ahhua",12);
Dog dog2=new Dog("a",15);
Dog dog3=new Dog("ah",13);
Dog dog4=new Dog("ah",13);

List list=new ArrayList();
list.add(dog2);
list.add(dog1);
list.add(dog2);
list.add(dog3);
//迭代器
Iterator iterator=list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());

}
for (Object o :list) {
System.out.println(o);
}


}
}
class Dog{
private String name;
private int age;

public Dog(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

image-20220704180742962