zoukankan      html  css  js  c++  java
  • 类排序

    方法一:实现Comparable接口

    public class Person implements Comparable<Person>
    {
        String name;
        int age;
        public Person(String name, int age)
        {
            super();
            this.name = name;
            this.age = age;
        }
        
        @Override
        public int compareTo(Person p)
        {
            return this.age-p.getAge();
        }
        
    }
    public static void main(String[] args)
        {
            Person[] people=new Person[]{new Person("xujian", 20),new Person("xiewei", 10)};
            System.out.println("排序前");
            for (Person person : people)
            {
                System.out.print(person.getName()+":"+person.getAge());
            }
            Arrays.sort(people);
            System.out.println("
    排序后");
            for (Person person : people)
            {
                System.out.print(person.getName()+":"+person.getAge());
            }
        }

    方法二:comparator

    Item[] items=new Item[]{new Item(3L, "aaaa"),new Item(2L, "bbbbb")};

    System.out.println("排序前");
    for (Item item : items)
    {
    System.out.print(item.getId()+":"+item.getDesc());
    }

    Arrays.sort(items, new Comparator<Item>() {
    @Override
    public int compare(Item o1, Item o2) {
    return o1.getId().compareTo(o2.getId());
    }
    });

    System.out.println(" 排序后");
    for (Item item : items)
    {
    System.out.print(item.getId()+":"+item.getDesc());
    }
  • 相关阅读:
    ffmpeg之AVFrame
    ffmpeg之samplefmt
    音视频基本概念
    cmake函数 file
    ffmpeg之AVPacket
    ffmpeg之AVFormatContext
    存储格式:packed和planar
    ffmpeg之channel_layout
    cmake函数: get_filename_component
    ffmpeg整体结构
  • 原文地址:https://www.cnblogs.com/chappell/p/10813162.html
Copyright © 2011-2022 走看看