Java notes 10__math class, Random class, arrays class/Date action Class/object comparer/

Source: Internet
Author: User
Tags comparable dateformat

/*** Math class, Random class, arrays class: Specific Java manual ... */ Public classMain { Public Static voidMain (string[] args) {string[] S1= {"A", "B", "C", "D", "E"}; string[] S2= {"A", "B", "C", "D", "E"}; Booleanb = Arrays.equals (S1,S2);//Compare two array contentsSystem.out.println (b);//Output Truestring[] S3=NewString[6]; Arrays.fill (S3,"Well Empty");        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 (); intindex = Arrays.binarysearch (S1, "C");    SYSTEM.OUT.PRINTLN (index); }}

/*** Date Action class * Dates class: Represents a specific moment, accurate to milliseconds, and is the current time when the program is running * Calendar class: Can be accurate to milliseconds * DateFormat class * SimpleDateFormat class: Date formatting class*/ Public classMain { Public Static voidMain (string[] args) {Date D1=NewDate (); Date C=NewDate (); System.out.println (D1.gettime ()); //milliseconds indicatesSystem.out.println (D1);//Format Representation//two ways to instantiateCalendar CA1 =calendar.getinstance (); Calendar Ca2=NewGregorianCalendar ();//Solar Calendar        intYear =Ca1.get (calendar.year); intmonth = Ca1.get (calendar.month) +1; intDay =Ca1.get (Calendar.day_of_month); inthour =Ca1.get (Calendar.hour); intminute =Ca1.get (Calendar.minute); intSecond =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 (NewDate ()); String Nowtime= Df2.format (NewDate ());        System.out.println (nowdate);                System.out.println (Nowtime); SimpleDateFormat SDF1=NewSimpleDateFormat ("Yyyy^mm^dd HH:mm:ss sss milliseconds"); String SWA= Sdf1.format (NewDate ());    SYSTEM.OUT.PRINTLN (SWA); }}

/*** Object comparer: Compare two or more data items to determine whether they are equal or determine the size relationship between them * Comparable interface: Forces the overall ordering of objects that implement each of its classes, and the CompareTo method of the class is called its natural comparison method * If the object of the custom class is to be sorted, then implement the comparable interface and implement the CompareTo method of the interface * * Comparable interface requires custom class to implement, according to OO principle: closed to the modification, open to the extension. * If this class is already defined and you don't want to change it, how do you do it? Answer: Arrays.sort has an overloaded method:*/ Public classMain { Public Static voidMain (string[] args) {string[] names= {"A", "B", "C", "D", "E", "F"}; person[] Persons= {            NewPerson ("a", 10),NewPerson ("B", 9),            NewPerson ("C", 8),NewPerson ("D", 7),            NewPerson ("E", 6),NewPerson ("F", 5),        }; //to sort an array of objects by a custom classarrays.sort (persons);  for(Person I:persons) System.out.println (i.name+ "" +i.age); Person2[] Persons2= {            NewPerson2 ("A", 10),NewPerson2 ("B", 9),            NewPerson2 ("C", 8),NewPerson2 ("D", 7),            NewPerson2 ("E", 6),NewPerson2 ("F", 5),        }; Arrays.sort (Persons2,NewPerson2comparator ());//using a custom comparer         for(Person2 i:persons2) System.out.println (i.name+ "" +i.age); System.out.println (arrays.tostring (PERSONS2));//returns a string representation of the contents of an array    }}classPersonImplementsComparable<person>{     PublicString name;  Public intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; } @Override Public intCompareTo (person O) {//Comparison method of comparators//implementation: Defining a comparison rule between objects        if(o==NULL){            Throw NewNullPointerException (); }        //returns 1 if the current object is less than the Parameter object//returns 0 if the current object equals a Parameter object//returns 1 if the current object is greater than the Parameter object        if( This. Age <o.age)return-1; Else if( This. Age <o.age)return1; return0; }}classperson2{ PublicString name;  Public intAge ; Person2 (String name,intAge ) {         This. Name =name;  This. Age =Age ; }}classPerson2comparatorImplementsComparator<person2>{@Override Public intCompare (Person2 O1, Person2 O2) {if(o1==NULL|| o2==NULL)           Throw NewNullPointerException (); if(O1.age <o2.age)return-1; Else if(O1.age >o2.age)return1; return0; }}

Java notes 10__math class, Random class, arrays class/Date action Class/object comparer/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.