Collections class method brief application

1. Search: binarySearch(List <?extends Comparable <?super T >>list, T key) When using this method, the elements in the container must be ordered.
2. Reverse the order of the elements in the specified list. reverse(List <?>list)
3.shuffle (List<?> list) (shuffle the order of elements in the container) permutes the specified list using the default random source

 4.swap(List<?> list, int i, int j)在指定列表的指定位置处交换元素。
package collictions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * collections类方法
 * 1.查找:binarySearch(List<? extends Comparable<? super T>> list, T key)使用此方法时容器内元素必须有序
 * 2.反转指定列表中元素的顺序。reverse(List<?> list)
 * 3.shuffle(List<?> list)(打乱容器中元素的顺序)使用默认随机源对指定列表进行置换
 * 4.swap(List<?> list, int i, int j)在指定列表的指定位置处交换元素。
 * @author Lenovo
 *
 */
public class CollctionsText {

    public static void main(String[] args) {
        List<Integer> list=new ArrayList<Integer>();
        for(int i=0;i<52;i++) {
            list.add(i);
        }
        Collections.shuffle(list);//打乱数字顺序

        List<Integer> p1=new ArrayList<Integer>();
        List<Integer> p2=new ArrayList<Integer>();
        List<Integer> p3=new ArrayList<Integer>();

        for(int b=0;b<51;b+=3) {//b=52时除不尽,所以还剩3个数,可再设一个对象将其求出
            p1.add(list.get(b));
            p2.add(list.get(b+1));
            p3.add(list.get(b+2));
        } 

        System.out.println(p1+"\n");
        System.out.println(p2+"\n");
        System.out.println(p3+"\n");

    }


}























Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860448&siteId=291194637