网站建设备案审核要多久,域名怎么建设网站,企业商标图案大全,广州网站建设有哪些公司一、概述
有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现#xff0c;而LinkedList实现基于linked allocation。
list接口提供了一些方法#xff1a; 二、The ArrayList and LinkedList Classes
1.构造方法
这两个类有相似的构造方法#xff1a…一、概述
有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现而LinkedList实现基于linked allocation。
list接口提供了一些方法 二、The ArrayList and LinkedList Classes
1.构造方法
这两个类有相似的构造方法
ArrayList()ArrayList(int initialCapacity)ArrayList(Collection? extends E c)
LinkedList()LinkedList(Collection? extends E c)
使用第三种构造方法可以将list转为arraylist
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class ArrayListDemo {public static void main(String[] args) {// 创建一个包含一些整数的列表ListInteger numbers Arrays.asList(1, 2, 3, 4, 5);// 使用 ArrayList(Collection? extends E c) 构造方法创建一个新的 ArrayList 对象ArrayListInteger arrayList new ArrayList(numbers);// 打印 ArrayList 的内容System.out.println(arrayList);}
}
用for循环将数组转为linkedlist
// Initialize array of names.String [] namesArray {Anna, Bob, Carlos, Debby};// Create empty list.LinkedListString list1 new LinkedList();// Populate the list using a loop.for (String s : namesArray){list1.add(s);}System.out.println(list1);
注意java中的collection都自带toString方法结果如下 [Anna, Bob, Carlos, Debby] 2.asList
我们可以不用for循环遍历而直接把一个array转化为linkedlist或arraylist
// Initialize array of names.
String[] namesArray {Anna, Bob, Carlos, Debby};// Create LinkedList from array using constructor.LinkedListString linkedList new LinkedList(Arrays.asList(namesArray));
也可以这样
LinkedList list2 new LinkedList(Arrays.asList(Anna, Bob, Carlos, Debby));
3.linkedlist中的其他方法 三、List Iterators
迭代器可以在列表中向前或向后移动。除了 next() 方法外列表迭代器还有一个 previous() 方法列表迭代器有一个光标位置它总是位于调用 previous() 方法返回的元素和调用 next() 方法返回的元素之间。
最初光标位于索引为 0 的元素之前。调用 next() 时光标将返回索引为 0 的元素并越过返回的元素向前移动到元素 0 和元素 1 之间的位置。此时调用 previous() 将返回元素 0并将光标移回起点但调用 next() 将返回元素 1并将光标向前移动到元素 1 和元素 2 之间的位置。如果光标已经越过最后一个元素调用 next() 将抛出 NoSuchElementException 异常。同样如果光标位置在元素 0 之前调用 previous() 也会产生同样的异常。
列表迭代器中的 remove() 方法会从列表中删除最后一次调用 next() 或 previous() 时返回的元素。
也就是说如果在调用 remove() 方法之前没有先调用 next() 或 previous() 方法那么会抛出一个 IllegalStateException 异常。
这是因为 remove() 方法的作用是删除迭代器最后访问的元素所以在调用 remove() 方法之前必须先调用 next() 或 previous() 方法以确保迭代器已经指向了要删除的元素。
如果在调用 remove() 方法之前没有先调用 next() 或 previous() 方法那么迭代器没有指向任何元素也就无法删除元素因此会抛出 IllegalStateException 异常。
这里的迭代器的remove方法和list的remove方法不是一个东西自然不能按原来的方式理解。
list的迭代器还提供了以下方法 例子
import java.util.*;/*** Demonstrates the list iterator*/
public class ListIteratorDemo {public static void main(String[] args) {// Create an array list to hold strings.String[] names {Chris, David, Katherine, Kenny};ListString nameList new ArrayList(Arrays.asList(names));// Display the names in the list.System.out.println(Here are the original names.);System.out.println(nameList);// Get a list iterator.ListIteratorString it nameList.listIterator();// Add Darlene to the list right after Chris.while (it.hasNext()) {String str it.next();// If the last name retrieved was Chris// then insert Darlene.if (str.equalsIgnoreCase(Chris)) {it.add(Darlene);// We are donebreak;}}//Display the names in the list again.System.out.println(\nHere are the new names now.);System.out.println(nameList);}
} Here are the original names. [Chris, David, Katherine, Kenny] Here are the new names now. [Chris, Darlene, David, Katherine, Kenny] 四、用list接口变量去引用linkedlist或arraylist
推荐使用
ListString nameList new ArrayList();
而不是 ArrayListString nameList new ArrayList();
因为第一种方法后期容易修改。
比如在后期发现namelist实际上应该使用linkedlist这是只需要把第一句改成
ListString myList new LinkedList();
这时项目中所有的myList就都变成了Linkedlist。
如果用第二种方法我们在项目中别的地方传参的时候会传入arraylist nameList这时如果要换成Linkedlist我们需要改变所有传入参数的类型。
使用第一种方法我们传的参数是List nameList就不需改变。