1. List 接口基本介绍
- List接口是 Collection 接口的子接口
- List集合类中元素有序(即添加顺序和取出顺序一致),且可重复。
List list = new ArrayList();
list.add("xdr");
list.add("mike");
list.add("jack");
list.add("lucy");
list.add("lucy");
System.out.println("list=" + list);
- List集合中的每个元素都有其对应的顺序索引,即支持索引。
List list = new ArrayList();
list.add("xdr");
list.add("mike");
list.add("jack");
list.add("lucy");
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.get(0));
- List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。【如上】
- JDK API中List接口的实现类有:
常用的有:ArrayList、LinkList和Vector
2. List 接口的常用方法
- List集合里添加了一些根据索引来操作集合元素的方法
- void add(int index, Object ele):在index位置插入ele元素
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
- boolean addAll(int index,Collection eles):从index位置开始将eles中的所有元素添加进来
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
- Object get(int index):获取指定index位置的元素
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
- int indexOf(Object obj):返回obj在集合中首次出现的位置
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
System.out.println(list.indexOf("jack"));
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.indexOf("lucy"));
- int lastlndexOf(Object obj):返回obj在当前集合中末次出现的位置
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
System.out.println(list.indexOf("jack"));
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.indexOf("lucy"));
list.add("兮动人");
System.out.println("list=" + list);
System.out.println(list.lastIndexOf("兮动人"));
- Object remove(int index):移除指定index位置的元素,并返回此元素
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
System.out.println(list.indexOf("jack"));
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.indexOf("lucy"));
list.add("兮动人");
System.out.println("list=" + list);
System.out.println(list.lastIndexOf("兮动人"));
list.remove(0);
System.out.println("list=" + list);
- Object set(int index, Object ele):设置指定index位置的元素为ele,相当于是替换.
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
System.out.println(list.indexOf("jack"));
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.indexOf("lucy"));
list.add("兮动人");
System.out.println("list=" + list);
System.out.println(list.lastIndexOf("兮动人"));
list.remove(0);
System.out.println("list=" + list);
list.set(1, "杰西卡");
System.out.println("list=" + list);
- List subList(int fromlndex, int tolndex):返回从fromIndex到tolndex位置的子集合
注意:返回的子集合是一个 前闭后开 的区间 [a , b) , formIndex <= subList < toIndex
List list = new ArrayList();
list.add("兮动人");
list.add("xdr630");
list.add(1, "云兮动人");
System.out.println("list=" + list);
List list2 = new ArrayList();
list2.add("jack");
list2.add("lucy");
list.addAll(1, list2);
System.out.println("list=" + list);
System.out.println(list.get(1));
System.out.println(list.indexOf("jack"));
list.add("lucy");
System.out.println("list=" + list);
System.out.println(list.indexOf("lucy"));
list.add("兮动人");
System.out.println("list=" + list);
System.out.println(list.lastIndexOf("兮动人"));
list.remove(0);
System.out.println("list=" + list);
list.set(1, "杰西卡");
System.out.println("list=" + list);
List returnList = list.subList(0, 2);
System.out.println(returnList);
3. 练习
- 添加10个以上的元素((比如String “hello”),在2号位插入一个元素"xdr",获得第5个元素,删除第6个元素,修改第7个元素,在使用迭代器遍历集合,要求:使用List的实现类ArrayList完成。
List list = new ArrayList();
for (int i = 0; i < 12; i++) {
list.add("hello" + i);
}
System.out.println("list=" + list);
list.add(1, "兮动人");
System.out.println("list=" + list);
System.out.println("第五个元素=" + list.get(4));
list.remove(5);
System.out.println("list=" + list);
list.set(6, "lucy");
System.out.println("list=" + list);
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj=" + obj);
}
4. List 的三种遍历方式 [ArrayList, LinkedList,Vector]
List list = new ArrayList();
list.add("xdr");
list.add("兮动人");
list.add("jack");
list.add("lucy");
System.out.println("====迭代器====");
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println(obj);
}
System.out.println("====增强for====");
for (Object o : list) {
System.out.println(o);
}
System.out.println("====普通for====");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
5. 实现类的练习
- 使用List的实现类添加三本图书,并遍历,打印如下效果
名称:xx 价格:xx 作者:xx
名称:xx 价格:xx 作者:xx
名称:xx 价格:xx 作者:xx
- 按价格排序从低到高(使用冒泡法)
- 要求使用ArrayList、LinkedList 和Vector 三种集合实现
public class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "名称:" + name + "\t\t作者:" + author + "\t\t价格:" + price;
}
}
public class ListExercise02 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Book("西游记", "吴承恩", 100));
list.add(new Book("红楼梦", "曹雪芹", 70));
list.add(new Book("水浒传", "施耐庵", 80));
list.add(new Book("三国演义", "罗贯中", 90));
for (Object o : list) {
System.out.println(o);
}
sort(list);
System.out.println("===排序后===");
for (Object o : list) {
System.out.println(o);
}
}
public static void sort(List list) {
int listSize = list.size();
for (int i = 0; i < listSize - 1; i++) {
for (int j = 0; j < listSize - 1 - i; j++) {
Book book1 = (Book) list.get(j);
Book book2 = (Book) list.get(j + 1);
if (book1.getPrice() > book2.getPrice()) {
list.set(j, book2);
list.set(j + 1, book1);
}
}
}
}
}
|