• 链表排序,查找,洗牌,旋转


    链表的排序和查找可以使用Collections类

    排序,public static sort(List<E> list1)不实现Comparable的结点不能用

    查找,int binarySearch(List<E> list2,T Key,CompareTo<T>) 

    都是第一个放链表对象,第二个放结点

    结点要实现接口Comparable接口的compareTo方法作为比较标准,后面的参数可以写null

    (比如Sring内部已经实现了compareTo方法,就是按照字典顺序排)

    测试代码

    package cgfg;
    
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Test{
        public static void main(String args[]){
            LinkedList<Student> list1=new LinkedList<Student>();
            list1.add(new Student("huang",4));
            list1.add(new Student("yu",2));
            list1.add(new Student("cheng",5));
            Iterator<Student> iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.println(student.getHeight());
            }
            Collections.sort(list1);
            iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.println(student.getHeight());
            }
            Student test=new Student("dd",5);
            int a=Collections.binarySearch(list1, test, null);
            Student b=list1.get(a);
            System.out.println(b.getName());
        }
    }
     
    class Student implements Comparable{
        private String name;
        private int height;
        Student(String a,int b){
            name=a;
            height=b;
        }
        int getHeight(){
            return height;
        }
        String getName(){
            return name;
        }
        public int compareTo(Object b){
            Student b2=(Student)b;
            return height-b2.height;
        }
    }
    View Code

    洗牌

    public static void shuffle(List<E> list1)

    旋转

    public static void rotate(List<E> list1,int distance)//正数右转,负数左转

    调转

    public static void reverse(List<E> list1)

    测试代码

    package cgfg;
    
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Test{
        public static void main(String args[]){
            LinkedList<Student> list1=new LinkedList<Student>();
            list1.add(new Student("huang",4));
            list1.add(new Student("yu",2));
            list1.add(new Student("cheng",5));
            Iterator<Student> iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.print(student.getHeight());
            }
            System.out.println("");
            Collections.shuffle(list1);//打乱
            iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.print(student.getHeight());
            }
            System.out.println("");
            Collections.rotate(list1,2);//右旋两次
            iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.print(student.getHeight());
            }
            System.out.println("");
            Collections.reverse(list1);//调转
            iterator1=list1.iterator();
            for(;iterator1.hasNext();){
                Student student=iterator1.next();
                System.out.print(student.getHeight());
            }
        }
    }
     
    class Student implements Comparable{
        private String name;
        private int height;
        Student(String a,int b){
            name=a;
            height=b;
        }
        int getHeight(){
            return height;
        }
        String getName(){
            return name;
        }
        public int compareTo(Object b){
            Student b2=(Student)b;
            return height-b2.height;
        }
    }
    View Code
  • 相关阅读:
    1130 Infix Expression (25分)
    1131 Subway Map (30分)
    1132 Cut Integer (20分)
    1133 Splitting A Linked List (25分)
    1134 Vertex Cover (25分)
    1135 Is It A Red-Black Tree (30分)
    tensorflow 1.0的部分项目配置匹配
    1136 A Delayed Palindrome (20分)
    谷粒商城Redisson分布式锁(二十四)
    谷粒商城缓存(二十三)
  • 原文地址:https://www.cnblogs.com/vhyc/p/6106959.html
Copyright © 2020-2023  润新知