Collections类方法

2017/08/12 10:57

Collections类方法调用详解

1.排序(Sort)

double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
}
Collections.sort(list);
for (int i = 0; i < array.length; i++) {
System.out.println(li.get(i));
}
//结果：112,111,23,456,231

2.混排（Shuffling）

Collections.Shuffling(list)
double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
}
Collections.shuffle(list);
for (int i = 0; i < array.length; i++) {
System.out.println(li.get(i));
}
//结果：112,111,23,456,231

3.反转(Reverse)

Collections.reverse(list)

double array[] = {112, 111, 23, 456, 231 };
for (int i = 0; i < array.length; i++) {
}
Collections. reverse (list);
for (int i = 0; i < array.length; i++) {
System.out.println(li.get(i));
}
//结果：231,456,23,111,112

4.替换所以的元素(Fill)

String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<str.length;j++){
}
Collections.fill(li,"aaa");
for (int i = 0; i < li.size(); i++) {
System.out.println("list[" + i + "]=" + li.get(i));

}
//结果：aaa,aaa,aaa,aaa,aaa

5.拷贝(Copy)

Collections.copy(list,li): 前面一个参数是目标列表 ,后一个是源列表。

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
double arr[] = {1131,333};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
}
Collections.copy(list,li);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：1131,333,23,456,231

6.返回Collections中最小元素(min)

collection中的所有元素都必须是通过指定比较器可相互比较的。
Collections.min(list)

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
Collections.min(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：23

7.返回Collections中最小元素(max)

collection中的所有元素都必须是通过指定比较器可相互比较的。
Collections.max(list)

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
Collections.max(list);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：456

8) lastIndexOfSubList

int count = Collections.lastIndexOfSubList(list,li);

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
}
Int locations = Collections. lastIndexOfSubList (list,li);
System.out.println(“===”+ locations);
//结果 3

9) IndexOfSubList

int count = Collections.indexOfSubList(list,li);

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
List li = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
double arr[] = {111};
String str[] = {"dd","aa","bb","cc","ee"};
for(int j=0;j<arr.length;j++){
}
Int locations = Collections.indexOfSubList(list,li);
System.out.println(“===”+ locations);
//结果 1

10) Rotate

Collections.rotate(list,-1);

double array[] = {112, 111, 23, 456, 231 };
List list = new ArrayList();
for (int i = 0; i < array.length; i++) {
}
Collections.rotate(list,-1);
for (int i = 0; i <list.size(); i++) {
System.out.println("list[" + i + "]=" + list.get(i));
}
//结果：111,23,456,231,112

0
0 收藏

2017/08/12 16:02

1 评论
0 收藏
0