站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题java集合类中几个鲜为人知的实用方法

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

摘要: java集合类中几个鲜为人知的实用方法

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="86" align="left" valign="top"> </td> </tr> </table>
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);
}
}
}

运行结果:

C:\java>java TestCollections
[a, b]
[t, a, n, k, s]
[t, k, n, a, s]
!!! 6. Found from [a, b, c, d, e, a, b, c, d, e] with [b, c, d]
!!! Not found from [a, b, c, d, e, a, b, c, d, e] with [b, d, d]
[t, a, n, k, s]
[s, t, a, n, k]
[t, a, n, k, s]
[s, t, a, n, k]
[a, b, c, d, e]
[a, c, d, b, e]
[t, a, n, a, s]
[t, hello, n, hello, s]
true
false
!!! 1. Found from [a, b, c, d, e] with [b, c, d]
!!! Not found from [a, b, c, d, e] with [b, d, d]

<table cellSpacing=0 border=0> </table>
</td> </tr> <tr>


↑返回目录
前一篇: 使用JDK6 中的JConsole监控应用
后一篇: 优秀软件开发人员的五个好习惯