Collections工具类
程序员文章站
2022-03-01 15:08:32
...
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Collections工具类 {
public static void main(String[] args) {
List<String> l = new ArrayList<>();
for(int i = 0;i < 10; i++) {
l.add("A"+i);
}
for(Iterator<String> it = l.iterator(); it.hasNext();) {
String s = it.next();
System.out.print(s+" ");
}
System.out.println();
Collections.reverse(l);//倒序
for(Iterator<String> it = l.iterator(); it.hasNext(); ) {
String s = it.next();
System.out.print(s+" ");
}
System.out.println();
Collections.shuffle(l);//乱序
System.out.println(l);
Collections.sort(l);//排序
System.out.println(l);
System.out.println(Collections.binarySearch(l, "A4"));//折半查找,返回索引
}
}