Java基礎之:集合——Collection——List

List簡單介紹
List介面是Collection的子介面,
List集合是有序的(輸入和輸出順序不變),且允許重復元素存在,
List集合每個元素都有其對應的順序索引,即List支持索引,
List使用及常用方法
首先是所有Collection中的方法在List中都可以使用,其次List 集合里添加了一些根據索引來操作集合元素的方法:
-
void add(int index, Object ele):在index位置插入ele元素
-
boolean addAll(int index, Collection eles):從index位置開始將eles中的所有元素添加進來
-
Object get(int index):獲取指定index位置的元素
-
int indexOf(Object obj):回傳obj在集合中首次出現的位置
-
int lastIndexOf(Object obj):回傳obj在當前集合中末次出現的位置
-
Object remove(int index):移除指定index位置的元素,并回傳此元素
-
Object set(int index, Object ele):設定指定index位置的元素為ele , 相當于是替換.
-
List subList(int fromIndex, int toIndex):回傳從fromIndex到toIndex位置的子集合
package class_List;
import java.util.List;
import java.util.ArrayList;
?
public class ClassTest01 {
?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
?
List list = new ArrayList();
for(int i = 1; i<=10;i++) {
list.add("hello" + "0" + i);
}
?
System.out.println(list);
//1. void add(int index, Object ele):在index位置插入ele元素
list.add(2, "hello川農");
System.out.println(list);
//2. boolean addAll(int index, Collection eles):將else從0到index位置之間的元素添加進來,回傳布林值
// list.addAll(8, list);
if(list.addAll(2, list)) System.out.println(list);
//3. Object get(int index):獲取指定index位置的元素
System.out.println(list.get(4));
//4. int indexOf(Object obj):回傳obj在集合中首次出現的位置
System.out.println(list.indexOf("hello03"));
//5. int lastIndexOf(Object obj):回傳obj在當前集合中末次出現的位置
System.out.println(list.lastIndexOf("hello02"));
//6. Object remove(int index):移除指定index位置的元素,并回傳此元素
Object obj = list.remove(2);
System.out.println(obj);
//7. Object set(int index, Object ele):設定指定index位置的元素為ele , 相當于是替換
System.out.println(list);
Object obj2 = list.set(0,"hi川農"); //回傳值:原list上指定index位置上的元素
System.out.println(obj2);
System.out.println(list);
//8. List subList(int fromIndex, int toIndex):回傳從fromIndex到toIndex位置的子集合
List list2 = list.subList(6, 8); //java中絕大部分范圍都是前閉后開的,即 [ 6,8)
System.out.println(list2);
}
}
List練習
添加10個以上的元素(比如String “hello” ),在2號位插入一個元素"川農", 獲得第5個元素,洗掉第6個元素,修改第7個元素,在使用迭代器遍歷集合,要求:使用List的實作類ArrayList完成,
package class_List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ClassWork01 {
?
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
?
List arrayList = new ArrayList();
for(int i = 1;i<10;i++) {
arrayList.add("hello" + i);
}
//獲得第5個元素,洗掉第6個元素,修改第7個元素
System.out.println(arrayList.get(4));
System.out.println("==============");
arrayList.remove(5);
arrayList.set(6, "hello川農");
//在2號位插入一個元素"川農",
arrayList.add(1, "川農");
Iterator iterator = arrayList.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator = arrayList.iterator();
}
}
List的三種遍歷方式
首先是Collection中的兩種遍歷方式,在List中都可以使用,即迭代器Iterator以及增強for回圈,
由于List支持索引,所以多了普通for回圈的遍歷方式,(與遍歷陣列一樣)
這里將三種方法都列出,用于復習Collection的兩種遍歷方式,
package class_List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ClassTest02_ForeachList {
?
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List list = new ArrayList();
for(int i = 1;i<4;i++) {
list.add("hello" + i);
}
System.out.println("=============方式一=============");
//方式一:迭代器
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator = list.iterator();
System.out.println("=============方式二=============");
//方式二:增強for回圈
for(Object obj:list) {
System.out.println(obj);
}
System.out.println("=============方式三=============");
//方式三:普通for回圈
for(int i = 0 ;i<list.size();i++) {
System.out.println(list.get(i));
}
}
}
簡單應用案例
使用List的實作類添加三本圖書,并遍歷,列印如下效果:
名稱:xx 價格:xx 作者:xx
名稱:xx 價格:xx 作者:xx
名稱:xx 價格:xx 作者:xx
要求: 按價格排序,從低到高(使用冒泡法) 要求使用LinkedList集合實作,
只要實作了List介面,那么List的實作類都可以使用List介面中的方法,
package class_ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ClassWork03 {
?
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
List linkedList = new LinkedList();
?
linkedList.add(new Book("三體1", 36.6, "劉慈欣"));
linkedList.add(new Book("三體2", 32.6, "劉慈欣"));
linkedList.add(new Book("三體3", 33.6, "大劉"));
?
for (Object obj : linkedList) {
System.out.println(obj);
}
?
sort(linkedList);
?
System.out.println("====================排序后==========================");
Iterator iterator = linkedList.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
?
}
?
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void sort(List linkedList) {
// 按價格排序,從低到高(使用冒泡法)
for (int i = 0; i < linkedList.size(); i++) {
for (int j = 0; j < linkedList.size() - 1 - i; j++) {
Book b1 = (Book) linkedList.get(j);
Book b2 = (Book) linkedList.get(j+1);
//使用List之后,不需要再像以前交換陣列元素一樣,使用臨時變數,
//上面b1,b2都已經保存了需要交換的元素
if(b1.getPrice() > b2.getPrice()) {
linkedList.set(j, b2);
linkedList.set(j+1, b1);
}
}
}
}
?
}
?
class Book {
private String name;
private double price;
private String author;
?
public Book(String name, double price, String author) {
super();
this.name = name;
this.price = price;
this.author = author;
}
?
public Book() {
super();
}
?
public String getName() {
return name;
}
?
public void setName(String name) {
this.name = name;
}
?
public double getPrice() {
return price;
}
?
public void setPrice(double price) {
this.price = price;
}
?
public String getAuthor() {
return author;
}
?
public void setAuthor(String author) {
this.author = author;
}
?
@Override
public String toString() {
return "名稱:" + name + "\t價格:" + price + "\t作者:" + author;
}
}
程式輸出
名稱:三體1 價格:36.6 作者:劉慈欣
名稱:三體2 價格:32.6 作者:劉慈欣
名稱:三體3 價格:33.6 作者:大劉
================排序后==================
名稱:三體2 價格:32.6 作者:劉慈欣
名稱:三體3 價格:33.6 作者:大劉
名稱:三體1 價格:36.6 作者:劉慈欣
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240454.html
標籤:Java
上一篇:python之亂數函式
