目录
List-toc" style="margin-left:0px;">一、List
List%E6%8E%A5%E5%8F%A3%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D-toc" style="margin-left:40px;">(一)List接口基本介绍
List%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95-toc" style="margin-left:0px;">二、List接口的常用方法
List%E9%9B%86%E5%90%88%E7%9A%84%E4%B8%89%E7%A7%8D%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F-toc" style="margin-left:0px;">三、List集合的三种遍历方式
四、小练习——使用冒泡排序遍历集合
一、List
List%E6%8E%A5%E5%8F%A3%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D">(一)List接口基本介绍
List接口是Collection接口的子接口
public interface List<E> extends Collection<E>
List%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95">二、List接口的常用方法
// 准备数据
List list = new ArrayList();
list.add("jack");
list.add("mary");
list.add("tom");
list.add("tom");// void add(int index, Object ele):在index位置插入元素
list.add(1, "john");
System.out.println("list=" + list);
// list=[jack, john, mary, tom, tom]// boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来
List list2 = new ArrayList();
list2.add("张三");
list2.add("李四");
list.addAll(3, list2);
System.out.println("list=" + list);
// list=[jack, john, mary, 张三, 李四, tom, tom]// get(int index):获取指定index位置的元素
System.out.println(list.get(5)); // tom// int indexOf(Object obj):返回obj在集合中首次出现的位置
System.out.println(list.indexOf("tom")); // 5// int lastindexOf(Object obj):返回obj在集合中末次出现的位置
System.out.println(list.lastIndexOf("tom")); // 6// Object remove(int index):移除指定index位置的元素,并返回此元素
Object remove = list.remove(3);
System.out.println("移除的元素是:" + remove); // 张三// Object set(int index, Object ele):设置指定index位置的元素为ele,相当于是替换
System.out.println("替换前:list=" + list);
// 替换前:list=[jack, john, mary, 李四, tom, tom]Object ele = list.set(3, "王五");
System.out.println(ele + "替换后:list=" + list);
// 李四替换后:list=[jack, john, mary, 王五, tom, tom]// List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合
// 注意:包头不包尾[fromIndex, toIndex)
List subList = list.subList(1, 5);
System.out.println("subList=" + subList);
// subList=[john, mary, 王五, tom]
List%E9%9B%86%E5%90%88%E7%9A%84%E4%B8%89%E7%A7%8D%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F">三、List集合的三种遍历方式
List list = new ArrayList();
// List list = new LinkedList();
// List list = new Vector();for (int i = 0; i < 12; i++) {list.add("hello" + i);
}
// 方式一:迭代器遍历
Iterator iterator = list.iterator();
while (iterator.hasNext()){Object obj = iterator.next();System.out.println(obj);
}// 方式二:增强for
for (Object obj : list) {System.out.println(obj);
}// 方式三:普通for循环
for (int i = 0; i < list.size(); i++) {Object obj = list.get(i);System.out.println(obj);
}
四、小练习——使用冒泡排序遍历集合
创建Book类:
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;}@Overridepublic String toString() {return "名称:" + name + "\t\t价格:" + price + "\t\t作者:" + author;}public double getPrice() {return price;}
}
创建集合:
public class ListExercise02 {public static void main(String[] args) {List list = new ArrayList();// List list2 = new LinkedList();// List list3 = new Vector();list.add(new Book("红楼梦", "曹雪芹", 100));list.add(new Book("西游记", "吴承恩", 10));list.add(new Book("水浒传", "施耐庵", 9));list.add(new Book("三国", "罗贯中", 80));list.add(new Book("西游记", "吴承恩", 10)); }
}
编写冒泡排序,从大到小排序:
public static void sort(List list) {for (int i = 0; i < list.size() - 1; i++) {for (int j = 0; j < list.size() - 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);}}}}
main方法中调用sort()方法:
// 排序前
for (Object o : list) {System.out.println(o);
}
sort(list);System.out.println("---------------排序后------------------");
// 排序后
for (Object o : list) {System.out.println(o);
}
运行结果: