Java中的集合类的几个鲜为人知的实用方法

本文介绍了Java集合框架中几个实用的方法,包括将Enumeration转换为List、交换List中元素位置、查找子列表首次出现的位置等,并提供了详细的示例代码。

1。list方法。   将 Enumeration 类型转换成list类型

2。swap方法。方便的调换一个list中的两个元素的位置。

3。lastIndexOfSubList方法。从一个list中从后面开始查找另外一个list第一次出现的位置。

4。rotate方法。在一个list中,顺序移动每一个元素的位置到指定的位置。

5。replaceAll方法。用指定的元素替换一个list中所用匹配的元素。

6。indexOfSubList方法。从一个list中从前面开始查找另外一个list第一次出现的位置。

示例程序:

import java.util.*;

class TestCollections {

public static void main(String[] args) {
TestCollections t = new TestCollections();
t.testList();
}
public void testList() {
Vector v = new Vector();
v.add("a");
v.add("b");
Enumeration e = v.elements() ;
List l = Collections.list(e);
System.out.println(l);
}

public void testSwap() {
List l = new ArrayList();
l.add("t");
l.add("a");
l.add("n");
l.add("k");
l.add("s");
System.out.println(l);
Collections.swap(l,1,3);
System.out.println(l);
}

public void testLastIndexOfSubList() {
List l = new ArrayList();
l.add("a");
l.add("b");
l.add("c");
l.add("d");
l.add("e");
l.add("a");
l.add("b");
l.add("c");
l.add("d");
l.add("e");
List l2 = new ArrayList();
l2.add("b");
l2.add("c");
l2.add("d");
int result = Collections.lastIndexOfSubList(l,l2);
if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
} else {
   System.out.println("!!! Not found from " + l + " with " + l2);
}
List l3 = new ArrayList();
l3.add("b");
l3.add("d");
l3.add("d");
result = Collections.lastIndexOfSubList(l,l3);
if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
} else {
   System.out.println("!!! Not found from " + l + " with " + l3);
}
}
public void testRotate() {
List l = new ArrayList();
l.add("t");
l.add("a");
l.add("n");
l.add("k");
l.add("s");
System.out.println(l);
Collections.rotate(l,1);
System.out.println(l);

//
   l = new ArrayList();
l.add("t");
l.add("a");
l.add("n");
l.add("k");
l.add("s");
System.out.println(l);
Collections.rotate(l,-4);
System.out.println(l);

   l = new ArrayList();
l.add("a");
l.add("b");
l.add("c");
l.add("d");
l.add("e");
System.out.println(l);
Collections.rotate(l.subList(1, 4), -1);
System.out.println(l);


}

public void testReplaceAll() {
List l = new ArrayList();
l.add("t");
l.add("a");
l.add("n");
l.add("a");
l.add("s");
System.out.println(l);
boolean b = Collections.replaceAll(l,"a","hello");
System.out.println(l);
System.out.println(b);

// not found
b = Collections.replaceAll(l,"a","hello");
System.out.println(b);

}

public void testIndexOfSubList() {
List l = new ArrayList();
l.add("a");
l.add("b");
l.add("c");
l.add("d");
l.add("e");
List l2 = new ArrayList();
l2.add("b");
l2.add("c");
l2.add("d");
int result = Collections.indexOfSubList(l,l2);
if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l2);
} else {
   System.out.println("!!! Not found from " + l + " with " + l2);
}
List l3 = new ArrayList();
l3.add("b");
l3.add("d");
l3.add("d");
result = Collections.indexOfSubList(l,l3);
if(result != -1) {
   System.out.println("!!! " + result + ". Found from " + l + " with " + l3);
} else {
   System.out.println("!!! Not found from " + l + " with " + l3);
}
}
}

转载于:https://www.cnblogs.com/jadmin/archive/2007/11/08/2206195.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值