java中List的用法和实例详解

本文详细介绍了Java集合框架中的List接口及其常用实现类Arraylist和Linkedlist的基本概念、常用方法以及使用注意事项,包括add(int index, object obj)方法与set(int index, object obj)方法的区别、sublist(int fromIndex, int toIndex)方法的应用,同时提供了实例代码进行演示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

list的用法

list包括list接口以及list接口的所有实现类。因为list接口实现了collection接口,所以list接口拥有collection接口提供的所有常用方法,又因为list是列表类型,所以list接口还提供了一些适合于自身的常用方法,如表1所示。

表1 list接口定义的常用方法及功能

从表1可以看出,list接口提供的适合于自身的常用方法均与索引有关,这是因为list集合为列表类型,以线性方式存储对象,可以通过对象的索引操作对象。

list接口的常用实现类有arraylist和linkedlist,在使用list集合时,通常情况下声明为list类型,实例化时根据实际情况的需要,实例化为arraylist或linkedlist,例如:

list l = new arraylist();// 利用arraylist类实例化list集合

list l2 = new linkedlist();// 利用linkedlist类实例化list集合

1.add(int index, object obj)方法和set(int index, object obj)方法的区别

在使用list集合时需要注意区分add(int index, object obj)方法和set(int index, object obj)方法,前者是向指定索引位置添加对象,而后者是修改指定索引位置的对象,例如执行下面的代码:

src\com\mwq\testcollection.java关键代码:

public static void main(string[] args) {

string a = "a", b = "b", c = "c", d = "d", e = "e";

list list = new linkedlist();

list.add(a);

list.add(e);

list.add(d);

list.set(1, b);// 将索引位置为1的对象e修改为对象b

list.add(2, c);// 将对象c添加到索引位置为2的位置

iterator it = list.iterator();

while (it.hasnext()) {

system.out.println(it.next());

}

}

在控制台将输出如下信息:

a

b

c

d

因为list集合可以通过索引位置访问对象,所以还可以通过for循环遍历list集合,例如遍历上面代码中的list集合的代码如下:

src\com\mwq\testcollection.java关键代码:

for (int i = 0; ilist = new linkedlist();

list.add(a);

list.add(e);

list.add(d);

list.set(1, b);// 将索引位置为1的对象e修改为对象b

list.add(2, c);// 将对象c添加到索引位置为2的位置

iterator it = list.iterator();

while (it.hasnext()) {

system.out.println(it.next());

}

//for (int i = 0; ilist = new arraylist();

list.add(a);// 索引位置为 0

list.add(repeat);// 索引位置为 1

list.add(b);// 索引位置为 2

list.add(repeat);// 索引位置为 3

list.add(c);// 索引位置为 4

list.add(repeat);// 索引位置为 5

list.add(d);// 索引位置为 6

system.out.println(list.indexof(repeat));

system.out.println(list.lastindexof(repeat));

system.out.println(list.indexof(b));

system.out.println(list.lastindexof(b));

}

src\com\mwq\testcollection.java完整代码如下:

package com.mwq;

import java.util.arraylist;

import java.util.list;

public class testcollection {

public static void main(string[] args) {

system.out.println("开始:");

string a = "a", b = "b", c = "c", d = "d", repeat = "repeat";

list list = new arraylist();

list.add(a); // 索引位置为 0

list.add(repeat); // 索引位置为 1

list.add(b); // 索引位置为 2

list.add(repeat); // 索引位置为 3

list.add(c); // 索引位置为 4

list.add(repeat); // 索引位置为 5

list.add(d); // 索引位置为 6

system.out.println(list.indexof(repeat));

system.out.println(list.lastindexof(repeat));

system.out.println(list.indexof(b));

system.out.println(list.lastindexof(b));

system.out.println("结束!");

}

}

在控制台将输出如下信息:

1

5

2

2

3.sublist(int fromindex, int toindex)方法

在使用sublist(int fromindex, int toindex)方法截取现有list集合中的部分对象生成新的list集合时,需要注意的是,新生成的集合中包含起始索引位置代表的对象,但是不包含终止索引位置代表的对象,例如执行下面的代码:

src\com\mwq\testcollection.java关键代码:

public static void main(string[] args) {

string a = "a", b = "b", c = "c", d = "d", e = "e";

list list = new arraylist();

list.add(a);// 索引位置为 0

list.add(b);// 索引位置为 1

list.add(c);// 索引位置为 2

list.add(d);// 索引位置为 3

list.add(e);// 索引位置为 4

list = list.sublist(1, 3);// 利用从索引位置 1 到 3 的对象重新生成一个list集合

for (int i = 0; ilist = new arraylist();

list.add(a); // 索引位置为 0

list.add(b); // 索引位置为 1

list.add(c); // 索引位置为 2

list.add(d); // 索引位置为 3

list.add(e); // 索引位置为 4

list = list.sublist(1, 3);// 利用从索引位置 1 到 3 的对象重新生成一个list集合

for (int i = 0; i


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值