1. Date
Import Java. util. date; public class datetimedisplay {date objdate; datetimedisplay () {objdate = new date ();} void display () {string strdate, strtime = ""; system. out. println ("today's date is:" + objdate); long time = objdate. gettime (); system. out. println ("time in milliseconds since January 1, January 1, 1970 (GMT):" + time); strdate = objdate. tostring (); strtime = strdate. substring (11, (strdate. length ()-4); strtime = "time:" + strtime. substring (0, 8); system. out. println (strtime) ;}} public class datetest2 {public datetest2 () {}public static void main (string [] ARGs) {datetimedisplay objdatetime = new datetimedisplay (); objdatetime. display ();}}
Output:
Today's date is: Fri Jul 04 15:02:51 CST 2008
Time in milliseconds since January 1, January 1, 1970 (GMT): 1215154971375
Time: 15: 02: 51
Example 2 format time
Package my; import Java. util. date; import Java. util. locale; import Java. text. dateformat; Class datetime {date objdate; datetime () {objdate = new date ();} void convert () {dateformat simpleformat = dateformat. getdateinstance (dateformat. long, locale. chinese); string strdate = objdate. tostring (); system. out. println ("current date:" + strdate); system. out. println ("/n formatted:" + simpleformat. format (objdate);} class datetimetest {protected datetimetest () {} public static void main (string [] ARGs) {datetime objdatetime = new datetime (); objdatetime. convert ();}}
2. The calendar class is an abstract class. You cannot instantiate objcalendar = calendar. getinstance () Like date ();
3 random class
New random (). nextfloat (); regenerate a random number between 0.0 and 1.0.
Or use math. Random () to generate a random number between 0.0 and 1.0.
4. arraylist class
Import Java. util. arraylist; import Java. util. random; import Java. util. list; import Java. util. collections; Class playerslist {arraylist playerarray; List sublistobj; List otherlistobj; playerslist () {playerarray = new arraylist (); sublistobj = new arraylist ();} void add () {for (int ctr = 0; CTR <5; CTR ++) {playerarray. add (New INTEGER (CTR);} playerarray. add ("Martina"); playerarray. add ("Serena "); Playerarray. add ("Venus"); playerarray. add ("Serena"); system. out. println (playerarray);} void display () {for (int ctr = 0; CTR <playerarray. size (); CTR ++) {system. out. print ("" + playerarray. get (CTR);} system. out. println ();} void search () {system. out. println ("the first time the string Serena appears in the position" + playerarray. indexof ("Serena"); system. out. println ("the first time the string Serena appears in the position" + playerarray. lastindexof ("Serena "));}/ * ** Extract a sublist from arraylist */void extract () {sublistobj = playerarray. sublist (5, playerarray. size (); system. out. println ("index 5 to" + playerarray. the new sublist of size () + "is:" + sublistobj);}/*** randomly changed sequence */void shuffle () {system. out. println ("Player list (Before):" + playerarray); collections. shuffle (playerarray, new random (); system. out. println ("Player list (later):" + playerarray);}/*** sorts the arraylist. */void sort () {System. out. println ("Before sorting:" + playerarray); collections. sort (bookarray); system. out. println ("sorted:" + playerarray);}/*** reverse arraylist. */void reverse () {system. out. println ("before reversal:" + playerarray); collections. reverse (bookarray); system. out. println ("reverse:" + playerarray);} void copy () {system. out. println ("is playerarray empty? "+ Playerarray. isempty (); system. out. println ("playerarray (Before):" + playerarray); bookcopyobj = new arraylist (bookarray); system. out. println ("otherlistobj (later):" + otherlistobj) ;}} class playerlisttest {public static void main (string [] ARGs) {playerslist playerobj = new playerslist (); playerobj. add (); playerobj. display (); playerobj. search (); playerobj. extract (); playerobj. shuffle (); playerobj. sort (); playerobj. reverse (); playerobj. copy ();}}
4.2shortlist
Method addlast ();
Removefirst ();
Getfirst (); get the first element of the queue.
Output:
[Dog, dog, cat, AAT, AAT, B4]
[Dog, cat, B4, AAT, aAt] is a random order.
4.3 hashmap key value comparison table
Import Java. util. *; public class printingcontainers {static map fill (MAP m) {M. put ("dog2", "Bosco"); // dog2 is the key, Bosco is the value of map. entry to obtain the key value m. put ("dog", "spot"); M. put ("cat", "Rags"); Return m;} public static void main (string [] ARGs) {system. out. println (fill (New hashmap ()));}}
Output:
{Dog2 = Bosco, dog = spot, cat = rags}
5. Vector class
Import Java. util. vector; Class preciousstones {vector preciousvector; preciousstones () {preciousvector = new vector ();} void add () {preciousvector. addelement ("Emerald"); preciousvector. addelement (""); preciousvector. addelement ("Emerald"); preciousvector. addelement (" green");} void insert () {preciousvector. insertelementat ("diamond", 0); preciousvector. insertelementat ("Maoyan", 4);} void display (String title) {int COUNT = 0; while (count <preciousvector. size () {system. out. print (preciousvector. elementat (count); count ++; If (count <preciousvector. size () {system. out. print (",") ;}}void search () {If (preciousvector. contains ("diamond") {system. out. println ("in index" + preciousvector. indexof ("diamond") + "") ;}} void remove () {preciousvector. removeelement (""); system. out. println ("content after yellow crystal deletion:"); int COUNT = 0; while (count <preciousvector. size () {system. out. print (preciousvector. elementat (count); count ++; If (count <preciousvector. size () {system. out. print (",") ;}}void otherdetails () {system. out. println ("/n first element =" + preciousvector. firstelement (); system. out. println ("Default capacity =" + preciousvector. capacity () ;}} class preciousstonetest {public static void main (string [] ARGs) {preciousstones objprecious = new preciousstones (); objprecious. add (); objprecious. display ("display content"); objprecious. insert (); objprecious. display ("display inserted content"); objprecious. search (); objprecious. remove (); objprecious. otherdetails ();}}
6. iterator class
Hasnext ()
Next ()
Remove ()
Example of comparison between list and set import Java. util. *; public class printingcontainers {static collection fill (collection C) {C. add ("dog"); C. add ("dog"); C. add ("cat"); C. add ("AAT"); C. add ("AAT"); C. add ("B4"); Return C;} public static void main (string [] ARGs) {// list stores a group of elements in a specific order. // set elements cannot be duplicated. out. println (fill (New arraylist (); system. out. println (fill (New hashset ()));}}