1.sort(Collection)方法的使用(含义:对集合进行排序)。
public class Practice {
public static void main(String
[] args
){
List c
= new ArrayList();
c
.add("l");
c
.add("o");
c
.add("v");
c
.add("e");
Collections
.sort(c
);
System
.out
.println(c
);
}
}
运行结果为:[e, l, o, v]
2.shuffle(Collection)方法的使用(含义:对集合进行随机排序)。
public class Practice {
public static void main(String
[] args
){
List c
= new ArrayList();
c
.add("l");
c
.add("o");
c
.add("v");
c
.add("e");
Collections
.shuffle(c
);
System
.out
.println(c
);
Collections
.shuffle(c
);
System
.out
.println(c
);
}
}
运行结果为: [l, v, e, o] [o, v, e, l]
3.reverse()方法的使用(含义:反转集合中元素的顺序)。
public class Practice {
public static void main(String
[] args
){
List list
= Arrays
.asList("one two three four five six siven".split(" "));
System
.out
.println(list
);
Collections
.reverse(list
);
System
.out
.println(list
);
}
}
运行结果为: [one, two, three, four, five, six, siven] [siven, six, five, four, three, two, one]
4.binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引)。
public class Practice {
public static void main(String
[] args
){
List c
= new ArrayList();
c
.add("l");
c
.add("o");
c
.add("v");
c
.add("e");
System
.out
.println(c
);
int m
= Collections
.binarySearch(c
, "o");
System
.out
.println(m
);
}
}
运行结果为: [l, o, v, e] 1
5.binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引)。
public class Practice {
public static void main(String
[] args
){
List c
= new ArrayList();
c
.add("l");
c
.add("o");
c
.add("v");
c
.add("e");
System
.out
.println(c
);
int m
= Collections
.binarySearch(c
, "o");
System
.out
.println(m
);
}
}
运行结果为: [l, o, v, e] 1
6.copy(List m,List n)方法的使用(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素)。
public class Practice {
public static void main(String
[] args
){
List m
= Arrays
.asList("one two three four five six siven".split(" "));
System
.out
.println(m
);
List n
= Arrays
.asList("我 是 复制过来的哈".split(" "));
System
.out
.println(n
);
Collections
.copy(m
,n
);
System
.out
.println(m
);
}
}
运行结果为: [one, two, three, four, five, six, siven] [我, 是, 复制过来的哈] [我, 是, 复制过来的哈, four, five, six, siven]
7. fill(List list,Object o)方法的使用(含义:用对象o替换集合list中的所有元素)。
public class Practice {
public static void main(String
[] args
){
List m
= Arrays
.asList("one two three four five six siven".split(" "));
System
.out
.println(m
);
Collections
.fill(m
, "啊啊啊");
System
.out
.println(m
);
}
}
运行结果为: [one, two, three, four, five, six, siven] [啊啊啊, 啊啊啊, 啊啊啊,啊啊啊, 啊啊啊, 啊啊啊, 啊啊啊]