zoukankan      html  css  js  c++  java
  • java学习总结

    1 获得项目绝对路径

    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + path + "/";

     2 Java中List的排序

    第一种方法,就是list中对象实现Comparable接口,代码如下:

    public class Person implements Comparable<Person> {
        private String name;
        private Integer order;
     
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
     
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
     
        /**
         * @return the order
         */
        public Integer getOrder() {
            return order;
        }
     
        /**
         * @param order
         *            the order to set
         */
        public void setOrder(Integer order) {
            this.order = order;
        }
     
        @Override
        public int compareTo(Person arg0) {
            return this.getOrder().compareTo(arg0.getOrder());
        }
     
    }

    测试例子

    public static void main(String[] args) {
        List<Person> listA = new ArrayList<Person>();
        Person p1 = new Person();
        Person p2 = new Person();
        Person p3 = new Person();
     
        p1.setName("name1");
        p1.setOrder(1);
        p2.setName("name2");
        p2.setOrder(2);
        p3.setName("name3");
        p3.setOrder(3);
     
        listA.add(p2);
        listA.add(p1);
        listA.add(p3);
        Collections.sort(listA);
        for (Person p : listA) {
            System.out.println(p.getName());
        }
    }

    第二种方法,就是在重载Collections.sort方法,代码如下:

    public class Person {
        private String name;
        private Integer order;
     
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
     
        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
     
        /**
         * @return the order
         */
        public Integer getOrder() {
            return order;
        }
     
        /**
         * @param order
         *            the order to set
         */
        public void setOrder(Integer order) {
            this.order = order;
        }
     
    }
    public static void main(String[] args) {
            List<Person> listA = new ArrayList<Person>();
            Person p1 = new Person();
            Person p2 = new Person();
            Person p3 = new Person();
     
            p1.setName("name1");
            p1.setOrder(1);
            p2.setName("name2");
            p2.setOrder(2);
            p3.setName("name3");
            p3.setOrder(3);
     
            listA.add(p2);
            listA.add(p1);
            listA.add(p3);
             
            Collections.sort(listA, new Comparator<Person>() {
                public int compare(Person arg0, Person arg1) {
                    return arg0.getOrder().compareTo(arg1.getOrder());
                }
            });
             
            for (Person p : listA) {
                System.out.println(p.getName());
            }
        }

    两次的执行结果,都是按照person对象的order进行升序排序。

    两次执行的结果都是:
    
    name1
    
    name2
    
    name3
  • 相关阅读:
    iOS 开发中的争议(一)
    好用的第三方控件,Xcode插件(不断更新)
    Objective-C Runtime 运行时之四:Method Swizzling
    Objective-C Runtime 运行时之一:类与对象
    va_start和va_end使用详解
    [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
    IOS开发之----#import、#include和@class的区别
    ARC
    iOS所有常见证书,appID,Provisioning Profiles配置说明及制作图文教程
    WWDC笔记:2013 Session 201 Building User Interfaces for iOS 7
  • 原文地址:https://www.cnblogs.com/wangshuo1/p/5791702.html
Copyright © 2011-2022 走看看