java中Collections有什么用
一個(gè)Collections的例子
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
馬克-to-win: 在操縱ArrayList里面的內(nèi)容時(shí), 通常我們利用Collections。Collections是集合框架中的一個(gè)工具類??捎脕砼判?,反轉(zhuǎn)ArrayList里面的內(nèi)容。
例:1.1.3
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class TestMark_to_win {
public static void main(String args[]) {
ArrayList l = new ArrayList();
l.add("a");
l.add("c");
l.add("b");
l.add("d");
System.out.println(l + "\n");
Collections.reverse(l);
System.out.println(l + "\n");
Collections.sort(l);
System.out.println(l + "\n");
Iterator it = l.iterator();
String lll = "";
while (it.hasNext()) {
String kk = (String) it.next();
lll = lll + kk;
// if (kk.equals("jkc")) System.out.println("has jkc");
}
System.out.println(lll);
Collections.fill(l, "m");
System.out.println(l + "\n");
}
}
結(jié)果 is:
[a, c, b, d]
[d, b, c, a]
[a, b, c, d]
abcd
[m, m, m, m]