java中Collections有什么用
一個Collections的例子
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
馬克-to-win: 在操縱ArrayList里面的內容時, 通常我們利用Collections。Collections是集合框架中的一個工具類。可用來排序,反轉ArrayList里面的內容。
例: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");
}
}
結果 is:
[a, c, b, d]
[d, b, c, a]
[a, b, c, d]
abcd
[m, m, m, m]