Java static import, System class, date class, runtime class, Calendar class, Shuffle method in collections class, Math class

Source: Internet
Author: User
Tags shuffle

/*jdk1.5 version new Features (cont.): Static import Note: When the name of the class is the same, the specific package name needs to be specified, and when the name of the  method is the same, the specific class name needs to be specified. */import Java.util.*;import Static java.util.arrays.*;//imports all of the statics members in the Arrays tool class. Import static java.lang.system.*;//Imports all the static members in the system class, noting that the members in the system class are static. Class Staticimport {public static void main (string[] args) {int[] Arr={1,3,54,5};sort (arr); int Index=binarysearch (arr,3 ), SOP (Index), SOP (Arrays.tostring (arr));//The Arrays here cannot be omitted and must indicate that the ToString method in the Arrays tool class is being called. SOP (ToString (arr));//error here, because there is also the toString () method in object. This is called the ToString () method with no arguments in object. So it goes wrong. }public static void sop (Object obj) {//system.out.println (obj); out.println (obj);}}
/*system class: Methods and properties in this class are static. Out: standard output, which outputs information to the console by default. In: standard input, which receives keyboard information by default. Describe some of the system's information: How to obtain System attribute information: Properties GetProperties (); */import java.util.*;class Systemdemo {public static void main (string[] args) {Properties prop=system.getproperties (); System.setproperty ("MyKey", "myvalue");//Customize the information in the system. String value=system.getproperty ("Os.name");//Gets the value of some specified parameter. System.out.println ("!!!!!!!!!!!!!!!" +value); String v=system.getproperty ("haha"); System.out.println (v); for (Object Obj:prop.keySet ()) {string str= (string) prop.get (obj); System.out.println (obj+ "..." +str);}}

/*date object: */import java.util.*;import java.text.*;class datedemo {public static void main (string[] args) {Date D=new date ( ); SOP (d);//Printing time can not understand, I hope some format SimpleDateFormat sdf=new SimpleDateFormat ("yyyy years mm month DD Day a:h m minutes s seconds");  Define date format//Call the Format method to let the pattern format specify a Date object. String Time=sdf.format (d);//execution of the format operation SOP (time);} public static void Sop (Object obj) {System.out.println (obj);}}

/*runtime object: Note that the class does not have a constructor, theoretically its method should be static, but found that many methods are not static. So this class must have a static method, the GetRuntime () method, that returns an object of this class type. It can be seen from this class that the class uses a singleton design pattern. */class Runtimedemo {public static void main (string[] args) throws Exception{runtime rt=runtime.getruntime (); Process p=rt.exec ("notepad.exe Systemdemo.java");//thread.sleep (8000);////p.destroy ();}}
//calendar abstract class Import Java.util.*;class CalendarDemo2 {public static void main (string[] args) {Calendar c=calendar.getinstance (); Printcalendar (c);//c.add (calendar.year,4);//c.add (calendar.month,3); C.add (calendar.day_of_month,-19); Printcalendar (c);} public static void Printcalendar (Calendar cal) {string[] months={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", " November "," December "};sop (Cal.get (calendar.day_of_month)); SOP (Cal.get (Calendar.day_of_week)); String[] weeks={"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};sop (Cal.get (calendar.year) + "Year" +months[cal.get ( Calendar.month)]+cal.get (calendar.day_of_month) + "Day" +weeks[cal.get (Calendar.day_of_week)]);} public static void Sop (Object obj) {System.out.println (obj);}} 
Import Java.util.*;class CalendarTest1 {public static void main (string[] args) {Calendar c=calendar.getinstance (); Gettimes (c); GetDay (c,2017);} public static void GetDay (Calendar c,int years) {c.set (year,2,1);//March 1 of a year. C.add (calendar.day_of_month,-1);//February 1 last day sop (C.get (Calendar.day_of_month));//output days}public static void Gettimes ( Calendar c) {C.add (calendar.day_of_month,-1), SOP (C.get (calendar.year) + "year" + (C.get (calendar.month) +1) + "Month" +c.get ( Calendar.day_of_month) + "Day" +c.get (calendar.hour) + ":" +c.get (Calendar.minute) + ":" +c.get (Calendar.second));} public static void Sop (Object obj) {System.out.println (obj);}}
Shuffle methods in the collections class:
/*static void Shuffle (list<?> list)//Use the default random source to displace the specified list. static void Shuffle (list<?> list, random rnd)  //Displace the specified list with the specified random source. */import java.util.*;class Shuffledemo {public static void main (string[] args) {list<string> list=new ArrayList <String> (), List.add ("Hello1"), List.add ("Hello2"), List.add ("Hello3"); List.add ("Hello4"); List.add ("Hello5") ); Collections.shuffle (list); SOP (list);} public static void Sop (Object obj) {System.out.println (obj);}}
Math class
Import Java.lang.*;import java.util.*;class Mathdemo {public static void main (string[] args) {randomtest ();} public static void Randomtest () {random r=new random (), for (int i=0;i<10;i++) {/*int x= (int) (Math.random () *10+1), SOP ( x); */int Y=r.nextint (Ten) +1;//random class to provide the method sop (y);}} public static void Test () {double D=math.ceil (13.98);//ceil returns the smallest integer greater than the specified double number (with decimal digits) Result:14.0sop (d);d ouble d1= Math.floor (16.78);//floor returns the largest integer (with decimal place) Result:16.0sop (D1) that is less than the specified double number, long L=math.round (15.87);//rounding SOP (L); Double D2=math.pow (2,3);//Index, 2 3-time Square. SOP ("d2=" +d2);} public static void Sop (Object obj) {System.out.println (obj);}}





Java static import, System class, date class, runtime class, Calendar class, Shuffle method in collections class, Math class

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.