码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/

时间:2014-12-07 00:09:17      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   for   

/**
 * Math类、Random类、Arrays类:具体查JAVA手册。。。。。。
 */
public class Main {
    public static void main(String[] args) {
        String[] s1 = {"a","b","c","d","e"};
        String[] s2 = {"a","b","c","d","e"};
        boolean b = Arrays.equals(s1,s2); //比较两数组内容
        System.out.println(b); //输出true
        
        String[] s3 = new String[6];
        Arrays.fill(s3,"井空");
        System.out.println(s3.length);
        for(String i:s3) System.out.print(i+" "); System.out.println();
        
        String[] s4 = Arrays.copyOfRange(s1, 2, 3); //[2,3)
        for(String i:s4) System.out.print(i+" "); System.out.println();
        
        int index = Arrays.binarySearch(s1, "c");
        System.out.println(index);
    }
}

 

/**
 * 日期操作类
 * Date类:表示特定的瞬间,精确到毫秒,也是程序运行时的当前时间
 * Calendar类:可以精确到毫秒
 * DateFormat类
 * SimpleDateFormat类:日期格式化类
 */
public class Main {
    public static void main(String[] args) {
        Date d1 = new Date();
        Date c = new Date();
        System.out.println(d1.getTime()); //毫秒表示
        System.out.println(d1); //格式表示
        
        //两种实例化方式
        Calendar ca1 = Calendar.getInstance();
        Calendar ca2 = new GregorianCalendar(); //阳历
        int year = ca1.get(Calendar.YEAR);
        int month = ca1.get(Calendar.MONTH)+1;
        int day = ca1.get(Calendar.DAY_OF_MONTH);
        int hour = ca1.get(Calendar.HOUR);
        int minute = ca1.get(Calendar.MINUTE);
        int second = ca1.get(Calendar.SECOND);
        System.out.println(year+" "+month+" "+day);
        System.out.println(hour+" "+minute+" "+second);
        
        DateFormat df1 = DateFormat.getDateInstance();
        DateFormat df2 = DateFormat.getTimeInstance();
        String nowDate = df1.format(new Date());
        String nowTime = df2.format(new Date());
        System.out.println(nowDate);
        System.out.println(nowTime);
        
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy^MM^dd HH:mm:ss SSS毫秒");
        String swa = sdf1.format(new Date());
        System.out.println(swa);
    }
}

 

/**
 * 对象比较器:对两个或多个数据项进行比较,以确定它们是否相等,或确定它们之间的大小关系
 * Comparable接口:强行对实现它的每个类的对象进行整体排序,类的compareTo方法被称为它的自然比较方法
 * 如果自定义类的对象要实现排序,那么就要实现Comparable接口,并实现接口的CompareTo方法
 * 
 * Comparable接口要求自定义类去实现,按照OO原则:对修改关闭,对扩展开放。
 * 如果这个类已经定义好,不想再去修改它,如何实现? 答:Arrays.sort有一个重载方法:
 */
public class Main {
    public static void main(String[] args) {
        String[] names = {"a","b","c","d","e","f"};
        Person[] persons = {
            new Person("a",10), new Person("b",9),
            new Person("c",8),  new Person("d",7),
            new Person("e",6),  new Person("f",5),
        };
        //自定义类对对象数组排序
        Arrays.sort(persons);
        for(Person i:persons) System.out.println(i.name+" "+i.age);
        
        
        Person2[] persons2 = {
            new Person2("a",10), new Person2("b",9),
            new Person2("c",8),  new Person2("d",7),
            new Person2("e",6),  new Person2("f",5),
        };
        Arrays.sort(persons2,new Person2Comparator()); //使用自定义的比较器
        for(Person2 i:persons2) System.out.println(i.name+" "+i.age);
        System.out.println(Arrays.toString(persons2));//返回数组内容的字符串表示形式
    }
}

class Person implements Comparable<Person>{
    public String name;
    public int age;
    Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Person o) { //比较器的比较方法
        //实现:定义对象之间的比较规则
        if(o==null){
            throw new NullPointerException();
        }
        //如果当前对象小于参数对象,返回-1
        //如果当前对象等于参数对象,返回0
        //如果当前对象大于参数对象,返回1
        if(this.age < o.age) 
            return -1;
        else if(this.age < o.age)
            return 1;
        return 0;
    }
}

class Person2{
    public String name;
    public int age;
    Person2(String name,int age){
        this.name = name;
        this.age = age;
    }
}
class Person2Comparator implements Comparator<Person2>{
    @Override
    public int compare(Person2 o1, Person2 o2) {
       if(o1==null || o2==null)
           throw new NullPointerException();
       if(o1.age < o2.age)
           return -1;
       else if(o1.age > o2.age)
           return 1;
       return 0;
    }
}

 

JAVA笔记10__Math类、Random类、Arrays类/日期操作类/对象比较器/

标签:style   blog   io   ar   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/fish7/p/4148984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!