李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
Java
正文
容器深入研究--List的功能方法
Leefs
2020-01-14 AM
1766℃
0条
# 容器深入研究--List的功能方法 ### 前言 本篇讲述《Java编程思想》第17.5小节,List的功能方法 ### 概述 正如你所看到的,基本的List很容易使用:大多数时候只是调用add()添加对象,使用get()一次取出一个元素,以及调用iterator()获取用于该序列的Iterator。 下面例子中每个方法都涵盖了一组不同的动作:basicTest()中包含每个List都可以执行的操作;iterMotion()使用Iterator遍历元素;对应的iterManipulation()使用Iterator修改元素;testVisual()用以查看List的操作效果;还有一些LinkedList专用的操作。 **代码示例** ```java public class Lists { private static boolean b; private static String s; private static int i; private static Iterator
it; private static ListIterator
lit; public static void basicTest(List
a) { a.add(1, "x"); // 在位置1添加 a.add("x"); // 在结尾添加 // 添加一个集合 a.addAll(Countries.names(25)); // 从3开始添加一个集合 a.addAll(3, Countries.names(25)); b = a.contains("1"); // 是否存在 // 整个集合都在里面吗 b = a.containsAll(Countries.names(25)); // 随机访问列表, ArrayList简单, LinkedList昂贵 s = a.get(1); // Get (typed) object at location 1 i = a.indexOf("1"); // Tell index of object b = a.isEmpty(); // Any elements inside? it = a.iterator(); // Ordinary Iterator lit = a.listIterator(); // ListIterator lit = a.listIterator(3); // Start at loc 3 i = a.lastIndexOf("1"); // Last match a.remove(1); // Remove location 1 a.remove("3"); // Remove this object a.set(1, "y"); // Set location 1 to "y" a.retainAll(Countries.names(25)); a.removeAll(Countries.names(25)); i = a.size(); a.clear(); } public static void iterMotion(List
a) { ListIterator
it = a.listIterator(); b = it.hasNext(); b = it.hasPrevious(); s = it.next(); i = it.nextIndex(); s = it.previous(); i = it.previousIndex(); } public static void iterManipulation(List
a) { ListIterator
it = a.listIterator(); it.add("47"); // Must move to an element after add(): it.next(); // Remove the element after the newly produced one: it.remove(); // Must move to an element after remove(): it.next(); // Change the element after the deleted one: it.set("47"); } public static void testVisual(List
a) { System.out.println(a); List
b = Countries.names(25); System.out.println("b = " + b); a.addAll(b); a.addAll(b); System.out.println(a); // Insert, remove, and replace elements // using a ListIterator: ListIterator
x = a.listIterator(a.size()/2); x.add("one"); System.out.println(a); System.out.println(x.next()); x.remove(); System.out.println(x.next()); x.set("47"); System.out.println(a); // Traverse the list backwards: x = a.listIterator(a.size()); while(x.hasPrevious()) { System.out.println(x.previous() + " "); } System.out.println(); System.out.println("testVisual finished"); } // There are some things that only LinkedLists can do: public static void testLinkedList() { LinkedList
ll = new LinkedList
(); ll.addAll(Countries.names(25)); System.out.println(ll); // Treat it like a stack, pushing: ll.addFirst("one"); ll.addFirst("two"); System.out.println(ll); // Like "peeking" at the top of a stack: System.out.println(ll.getFirst()); // Like popping a stack: System.out.println(ll.removeFirst()); System.out.println(ll.removeFirst()); // Treat it like a queue, pulling elements // off the tail end: System.out.println(ll.removeLast()); System.out.println(ll); } public static void main(String[] args) { // Make and fill a new list each time: basicTest( new LinkedList
(Countries.names(25))); basicTest( new ArrayList
(Countries.names(25))); iterMotion( new LinkedList
(Countries.names(25))); iterMotion( new ArrayList
(Countries.names(25))); iterManipulation( new LinkedList
(Countries.names(25))); iterManipulation( new ArrayList
(Countries.names(25))); testVisual( new LinkedList
(Countries.names(25))); testLinkedList(); } } ``` > 运行结果 ``` [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] b = [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, one, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] DJIBOUTI EGYPT [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, one, 47, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] LESOTHO KENYA COTE D’IVOIR (IVORY COAST) BISSAU GUINEA GHANA THE GAMBIA GABON ETHIOPIA ERITREA EQUATORIAL GUINEA EGYPT DJIBOUTI CONGO COMOROS CHAD CENTRAL AFRICAN REPUBLIC CAPE VERDE CAMEROON BURUNDI BURKINA FASO BOTSWANA BENIN ANGOLA ALGERIA LESOTHO KENYA COTE D’IVOIR (IVORY COAST) BISSAU GUINEA GHANA THE GAMBIA GABON ETHIOPIA ERITREA EQUATORIAL GUINEA 47 one CONGO COMOROS CHAD CENTRAL AFRICAN REPUBLIC CAPE VERDE CAMEROON BURUNDI BURKINA FASO BOTSWANA BENIN ANGOLA ALGERIA LESOTHO KENYA COTE D’IVOIR (IVORY COAST) BISSAU GUINEA GHANA THE GAMBIA GABON ETHIOPIA ERITREA EQUATORIAL GUINEA EGYPT DJIBOUTI CONGO COMOROS CHAD CENTRAL AFRICAN REPUBLIC CAPE VERDE CAMEROON BURUNDI BURKINA FASO BOTSWANA BENIN ANGOLA ALGERIA testVisual finished [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] [two, one, ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA, LESOTHO] two two one LESOTHO [ALGERIA, ANGOLA, BENIN, BOTSWANA, BURKINA FASO, BURUNDI, CAMEROON, CAPE VERDE, CENTRAL AFRICAN REPUBLIC, CHAD, COMOROS, CONGO, DJIBOUTI, EGYPT, EQUATORIAL GUINEA, ERITREA, ETHIOPIA, GABON, THE GAMBIA, GHANA, GUINEA, BISSAU, COTE D’IVOIR (IVORY COAST), KENYA] ``` 在basicTest()和iterMotion()方法中,调用只是为了演示正确的语法,虽然取得了返回值,却没有使用。某些情况则根本没有捕获返回值。
标签:
Java
,
Java编程思想
,
容器深入研究
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://lilinchao.com/archives/431.html
上一篇
容器深入研究--可选操作
下一篇
容器深入研究--Set和存储顺序
评论已关闭
栏目分类
随笔
2
Java
326
大数据
229
工具
31
其它
25
GO
47
NLP
2
标签云
线程池
Golang
正则表达式
微服务
MyBatisX
BurpSuite
Spark Streaming
Java编程思想
排序
DataWarehouse
SpringBoot
高并发
Eclipse
nginx
LeetCode刷题
散列
Ubuntu
MyBatis-Plus
字符串
前端
pytorch
持有对象
Elasticsearch
JavaWeb
Elastisearch
Beego
算法
CentOS
Tomcat
Flume
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞
评论已关闭