Java Util Practical Tool Category Library

xiaoxiao2021-03-06  78

Practical tool class library java.utilyesjoy reposted (Participate: 197, expert points: 70) Published: 2004-9-25 9:33 am Version: 1.0 Read: 581 times

This chapter describes the Java's utility library java.util package. In this package, Java provides some practical methods and data structures. For example, Java provides a date (DATA) class, Calendar class to generate and acquire date and time, providing random number (Random) class to generate various types of random numbers, but also provide stack, vector (Vector) , Bit set (BitSet) and hashtable, etc. to represent the corresponding data structure. Figure 8.1 shows the basic hierarchical diagram of the java.util package. Below we will specify several important classes. ┌Java.util.bitSet │Java.util.calendar │ └java.util.gregoriancalendar │Java.util.date │Java.util.dictionary │ └java.util.hashtable │ └java.util.properties │Java.util. EventObject │java.util.ResourceBundle ┌ general category ┤ ├java.util.ListResourceBundle │ │ └java.util.PropertyResourceBundle │ │java.util.Local │ │java.util.Observable │ │java.util.Random │ │java. util.StringTokenizer │ │java.util.Vector │ │ └java.util.Stack Java.util┤ └java.util.TimeZone │ └java.util.SimpleTimeZone │ ┌java.util.Enumeration ├ Interface ┤java.util.EventListener │ └java.util.Observer │ ┌java.util.EmptyStackException └ ┤java.util.MissingResourceException │java.util.NoSuchElementException └java.util.TooManyListenersException FIG exception class hierarchy 8.2 8.1 basic date date Java java.util package type In the day The information about the date and time is encapsulated in the class, and the user can obtain the system time or set the date and time by calling the corresponding method. There are many methods in the Date class that have been outdated after JDK1.0, and we will introduce other classes for newly added functions in JDK1.0 in JDK1.0. Six constructors are defined in the date class. (1) The datetime time of the date class object created by public Date () is set to the dateable time corresponding to the time. Example Date Today = New Date (); // Today is set to create a date corresponding to the time. (2) Public Date (long Date) The parameter DATE can be obtained by calling the Static method PARSE (String S) in the DATE class. Example long L = Date.Parse ("MON 6 JAN 1997 13: 3: 00"); Date Day = New Date (L); // DAY is Monday, January 6, 1997, 13: 3: 00 .

(3) Public Date (String S) Generate a date object by string S. The format of S is the same as the mode of string parameters in the method PARSE. Example Date Day = New Date ("MON 6 JAN 1997 13: 3: 00"); // DAY is Monday, January 6, 1997, 13: 3: 00. (4) Public Date (int year, INT MONTH, INT DATE) (5) Public Date (int year, int month, int Date, int hrs, int min) (6) Public Date (int Year, int Month, int Date, int HRS, INT min, int SEC ) Create a date object according to a given parameter. Parameter Description: The value of YEAR is: the year-1900 required to set. For example, the year to set is 1997, the value of YEAR should be 97, that is, the result of 1997-1900. So the date in Date is 400; the value domain of Month is 0 ~ 11, 0 represents January, 11 table represents December; Date's value domain between 1 ~ 31; HRS value domain is 0 ~ 23 between 23. From midnight to 1 o'clock in the second day, HRS = 0, from 1 noon to 1 in the afternoon, HRS = 12; MIN and SEC's value domain between 0 to 59. Example Date Day = New Date (11, 3, 4); // DAY is: 04-APR-11 12:00:00 AM In addition, incorrect parameters can also be given. The set time is February 30, 1910, which will be explained on March 2. Date Day = New Date (10, 1, 30, 10, 12, 34); System.out.Println ("Day's date is: day); // Print results are: day's date is: web mar 02 10: 13:34 GMT 08: 00 1910 Let's give some common methods in the DATE class. (1) Public Static Long Utc (int year, int month, int tent) This method will use a given parameter to calculate the UTC value. UTC is a timing system that is slightly different from the timing system of GMT (Greenwich Time). The UTC timing system is based on atomic clock, and the GTMT timing system is based on astronomical observations. The usual GMT timing system used is used. (2) Public Static Long Parse (String S) This method converts string S into a LONG type date. This method has been used when introducing the construction method Date (long Date). String S has a certain format, generally: (Sun GMT Time Zone), if the time zone is not indicated, the local time zone. (3) Public void setmonth (4) Public int getMonth () These two methods are set and acquired a monthly value. The value domain of the obtained month is from 0 to 11, 0 represents January, 11. December. (5) Public string toString () (6) Public String Tolocalstring () (7) Public String TogmTString () Converts a given date object into a string of different formats. The specific formats they correspond to the example 8.1. (8) Public int getTimezoneOffset () This method is used to obtain the time zone offset of the date object. The basic methods in the DATE class described above are specifically applicable in Example 8.1, and the corresponding results are printed.

Since some excessive methods are used, there is warning information when compiling. In addition, since the time in this example is related to the platform, the different JDK versions are not identical to this process, so the results of the different versions of JDK execution this example may be slightly different. Example 8.1 Dateapp.java Import Java.lang.system; Import Java.util.date; Public Class DateApp {Public Static Void Main (String Args []) {Date Today = New Date (); // Today's date is set The date and time of the creation time, assuming the creation time is 17:54 seconds from March // 23:00 on March 54, 1997. System.out.println ("Today's Date IS" Today); // Returns the general time representation Today's Date (Internet GMT) IS: " Today.togmtstring ()); // Return the result is the GMT time representation, this example is /// tDay's date (Internet GMT) IS: 23 May 1997 09:51:54 : Gmt system.out.println ("Today's Date (Locale) IS:" Today.tolocalestring ()); // Returns the time representation of local habits, the result is // Today's Date (Locale) IS: 05 / 23/97 17:51:54 System.out.println ("Today's Year IS:" Today.Getyear ()); System.out.Println ("Today's Month IS:" (Today.getMonth () 1) ); System.out.println ("Today's Date IS:" Today.Getdate ()); // Call the Date class method to get the value of the year. // Different constructors are called to create a DATE class.

Date day1 = new date (100, 1, 23, 10, 12, 34); System.out.Println ("Day1's Date IS: Day1); Date Day2 = New Date (" SAT 12 AUG 1996 13: 3: 00 "); system.out.println (" day2's date is: " day2); long L = Date.Parse (" SAT 5 AUG 1996 13: 3: 00 GMT 0800 "); Date Day3 = New Date (L ); System.out.println ("Day3's date (gmt) IS:" day3.togmtstring ()); System.out.Println ("Day3's Date (Locale) IS:" day3.tolocalestring ()); System. Out.println ("Day3's time zone offset is:" day3.gettimezoneoffset ());}} Run (JDK1.3, different from the original text, the original version is JDK1.0): E: / java / tutorial / java01 > Java Dateapp Today's Date Is Thu Dec 27 17:58:16 CST 2001 Today's Date (Internet GMT) IS: 27 DEC 2001 09:58:16 GMT Today's Date (Locale) IS: 2001-12-27 17:58:16 Today's Year I: 101 Today's Date IS: 27 Day1's Date IS: WED Feb 23 10:12:34 CST 2000 Day2's Date IS: fri aug 12 13:03:00 CST 1996 Day3's Date (GMT) IS: 5 aug 1996 05:03:00 GMT Day3's Date (Locale) IS: 1996-8-5 13:03:00 Day3's Time Zone Offset IS: -480 E : / java / tutorial / java01> 8.3 Calendar Calendar In the early JDK version, the Date (Date) class is attached to two features: (1) Allow the use of years, months, days, time, minute, seconds to explain the date: (2) Allow formatting and syntax analysis of strings indicating the date. The class Calendar is provided in JDK1.1 to complete the first function, class DateFormat to complete the second function. DateFormat is a class in the Java.Text package. Unlike the Date class, the DateFormat class accepts a date string represented by various languages ​​and different habits. This section describes the class Calendar in the java.util package and other newly added related classes. Class Calendar is an abstract class that completes the Date class and a common date representation (ie, a set of interstitial domains such as Year, Month, Day, Hour Representation Date). Due to the different rules used, different calendar systems differ from the interpretation of the same date. It provides a subclass of Calendar classes in JDK1.1, which implements the general calendar system in the world.

Of course, users can also inherit the Calendar class and increase the required rules to achieve different calendar systems. The Gregoriancalendar inherited the Calendar class. This section will introduce the relevant methods in the Calendar class while introducing class Gregoriancalendar. Class GregorianCalendar provides seven constructor: (1) The correlation value in the object created by Public GregorianCalendar () is set to specify the time zone, the current time of the default place, that is, the time zone at which the program is running, the current time of the location. (2) The correlation value in the object created by Public Gregoriancalendar is set to the current time of the time zone ZONE, the default location. (3) The correlation value in the object created by Public GregorianCalendar is set to the default time zone, the current time of the specified place alocale. (4) The correlation value in the object created by Public Gregoriancalendar (Timezone Zone) is set to the specified time zone, the current time of the specified location. The nature of the class Timezone used above is as follows: TimeZone is a class in the java.util package, which encapsulates information about the time zone. Each time zone corresponds to a set of IDs. The class Timezone provides some ways to complete the conversion between the time zone and the corresponding ID. (I) Knowing a particular ID, you can call the method public static synchronized timezone gettimezone (String ID) to get the corresponding time zone object. The ID of the Pacific Time Zone is PST, and the time zone object corresponding to the Pacific Time Zone can be obtained with the following method: Timezone Tz = Timezone.getTimezone ("PST"); calling method getDefault () can get the target of the time zone where the host is located. Timezone Tz = Timezone.getDefault (); (II) Call the following method You can get the ID ■ Public Static Synchronized string [] GetAvailableIDS (int RAWOFFSET) Gets an ID array based on a given time zone offset value. The IDs of the different regions of the same time may be different, which is due to the uniform in different regions to implement the summer time. String s [] = timezone.getavailableids (-7 * 60 * 60 * 1000); Print S, the result is S [0] = Pnt, S [1] = MST ■ public static synchronized string [] getavailableids () Get Offer All supported IDs. ■ Public String getId () Gets the ID of a specific time zone object. Example Timezone tz = timezone.getDefault (); string s = tz.getid (); Print S, result is S = CTT. The object used above represents a specific geographic, political or cultural area. Locale is just a mechanism that is used to identify a class of objects, and local itself does not contain such objects. To get an object of a Locale, there are two ways: (I) Call the constructor of the Locale class Locale (String Language, String Country) Locale (String Language, String Country, String Variant) Parameter Description: Language ?? in ISO-639 The defined code consists of two lowercase letters. The code defined in ISO-3166 consists of two uppercase letters in ISO-3166.

Variant® salesman and code for a particular browser, such as using WIN represents Windows. (II) Call the constant Local class defined in the Locale class provides a large amount of constant for users to create a Locale object. Example locale.china creates a Locale object for China. Other methods in class Timezone and class locale, readers can check the API. (5) Public Gregoriancalendar (int year, int month, int Date) (3) Public Gregoriancalendar (int year, int month, int Date, int hour, int minute) (7) Public Gregoriancalendar (int year, int month, int Date) , int HOUR, INT Minute, Int Second, create a GregorianCalendar object with a given date and time. Parameter Description: Year-Set the variable of the calendar object "MONTH-Set the variable of the calendar object Month; Date-Set the variable of the calendar object Date; Hour-Set the variable of the calendar object Hour_of_Day; minute- set the calendar object Variable Minute; Second- Sets the variable of the calendar object. Different from the DATE class is the value of Year does not have 1900, and Year's value represents the actual year. The meaning of Month is the same as the DATE class, and 0 represents January, 11 represents December. The date of Gregoriancalendar Cal = New Gregoriancalendar (1991, 2, 4) CAL is March 4, 1991. In addition to similar ways to Date, the Calendar class also provides a method of scrolling calculation and mathematical calculations on calendar. The calculation rule is determined by a given calendar system. When performing a date calculation, sometimes the information is insufficient or the information is not true. Calendar has a corresponding method to solve these problems. The default settings will be used when the information is insufficient, and the default settings in the GregorianCalendar class is generally Year = 1970, Month = January, Date = 1. When the information is not in real time, Calendar will first select the corresponding Calendar variable combination, and discard other conflictable information. MONTH DAY_OF_MONTH MONTH WEEK_OF_MONTH DAY_OF_WEEK MONTH DAY_OF_WEEK_OF_MONTH DAY_OF_WEEK DAY_OF YEAR DAY_OF_WEEK_WEEK_OF_YEAR HOUR_OF_DAY8.4 random number based Random Java class library utility java.util.Random provides methods for producing various types of random numbers. It generates int, long, float, double, and goussian equivalents. This is also the most different from the method random () in java.lang.math, and the latter only produces a Double type random number. The method in the class random is very simple, it has only two construction methods and six ordinary methods. Method: (1) Public Random () (2) Public Random (long seed) Java generates a base value Seed, in the first method default, the system time is used as SEED. Ordinary method: (1) Public synonized void setseed (long seed) This method is to set the base value SEED. (2) Public int nextint () This method is to generate an integer random number.

(3) Public long nextlong () This method is to generate a LONG type random number. (4) Public float nextfloat () This method is to generate a Float type random number. (5) Public Double nextdouble () This method is to generate a Double type random number. (6) Public synchronized double nextgoussian () This method is to generate a Double type Goussian random number. Example 8.2 Randomapp.java. // Import java.lang. *; import java.util.random; public class randomapp {public static void main (string args []) {random ran1 = new random (); random ran2 = new random (12345); // Create two objects of two class random. System.out.println ("The 1st set of random number); system.out.println (" / t integer: " ran1.nextint ()); system.out.println (" / t long: " Ran1.nextlong ()); system.out.println ("/ t float:" ran1.nextFloat (); system.out.println ("/ t Double:" ran1.nextdouble ()); system.out .println ("/ t Gaussian:" ran1.nextgaussian ()); // Generate various types of random number system.out.print ("THE 2nd set of random numbers:"); for (int i = 0; I <5; i ) {system.out.println (ran2.nextint () ""); if (i == 2) System.out.println (); // Generate different random numbers of the same type. System.out.println (); //}}} sic run results: E: / java01> java RandomApp The 1st set of random numbers: Integer: -173899656 Long: 8056223819738127077 Float: 0.6293638 Double: 0.7888394520265607 Gaussian: 0.5015701094568733 The 2nd Set of Random Numbers: 1553932502 -2090749135 -287790814 -355989640 -71685986 E: / java01> 8.5 vector class Vector Java.util.Vector provides a vector class to implement similar to dynamic arrays. In the Java language. As mentioned at the beginning, it is not a pointer concept, but if the pointer can be used correctly and flexibly, it is indeed greatly improved the quality of the program, such as the so-called "dynamic array" in C, C , is generally implemented by a pointer. In order to make up for this defect, Java provides a rich library to facilitate the programmer, and the VECTOR class is one of them. In fact, flexible use of arrays can also complete the functionality of the vector class, but the large number of methods provided in the quantity class greatly facilitate the user's use.

Once a vector class is created, you can insert different classes of different classes, neither the type of capacity is not required to be selected, and it is convenient to find. Use the vector class for pre-definition of the array size or not to pre-define the size of the array, and you can consider using the vector class frequently. Vector class provides three construction methods: Public Vector () Public Vector, int CapacityIncApacity, using the first method, the system automatically manages the vector object. If two methods are used, the system will set the capacity of the vector object according to the parameter initialcapacity (ie, the size of the vector object can store data). When the number of data that is really stored exceeds the capacity, the system will expand the storage capacity of the vector object. Parameters CapacityInCrement gives each expanded expansion value. When CapacityInCrement is 0, each time it is doubled. Use this feature to optimize storage. Various methods are provided in the Vector class Convenient users: ■ Insert function (1) Public final synchronized void addelement (Object Obj) Inserts OBJ into the tail of the vector. OBJ can be an object of any class. For the same vector object, different types of objects can be inserted therein. However, inserting should be an object rather than a value, it should be aware that the numerical value is converted into a corresponding object when inserting a value. When you want to insert an integer 1, don't call v1.addelement (1) directly, the correct method is: vector v1 = new vector (); integer integer1 = new integer (1); v1.addelement (integer1); (2) Public final synchronized void setElementat (Object Obj, int index) sets the objects at Index to OBJ, the original object will be overwritten. (3) Public final synchronized void insertlementat (Object Obj, int index) inserts OBJ, original object, and the object after INDEX. ■ Delete function (1) Public final synchronized void removeEleMent (Object Obj) Remove OBJ from the vector. If there is a plurality of existence, start trial from the vector head, delete the found vector member found with the OBJ. (2) Public Final Synchronized Void RemoveAllelement () Deletes all objects in the vector. (3) Public Final Synchronized Void RemoveElementLat (int index) Delete the object of the place referred to in Index. ■ Query Search Function (1) Public Final INDEXOF (Object Obj) Starts search OBJ from the vector head, returns the next subscript encountered, if this OBJ is not present, return -1. (2) Public final synchronized int indexof (Object Obj, int index) starts searching OBJ from the subscript represented by Index. (3) Public Final Int LastIndexof (Object Obj) Starts the reverse search of OBJ from the tail of the vector. (4) Public Final Synchronized Int LastIndexof (Object Obj, int index) is retrograde the OBJ from the head until the subscript represented by INDEX.

(5) Public Final Synchronized Object First FirstElement () Gets the first OBJ in the vector object. (6) Public Final Synchronized Object Lastelement () Gets the last OBJ in the vector object. After learning the most basic method of the vector, let's take a look at 8.3Vectorapp.java. Example 8.3 VectorApp.java.

Import java.util.vector; import java.lang. *; // This sentence should not, but the original text java.util.Enumeration; public class vectorApp {public static void main (string [] args) {Vector V1 = New Vector (); Integer Integer1 = New Integer (1); v1.addelement ("one"); // Add to string object v1.addelement (Integer1); v1.addelement (integer1); // joined Integer's object v1.addeElement ("Two"); v1.addelement (new integer (2)); v1.addelement (integer1); system.out.println ("the vector v1 is: / N / T " V1); // convert V1 into a string and print v1.insertelementat (" Three ", 2); v1.insertelementat (new float (3.9), 3); system.out.println (" THE Vector V1 (used method insertelementat ()) is: / n / t " v1); // Insert a new object into the specified location, and the object after the specified position is postponed later. V1.SETELEMENTAT (" FOUR ", 2); System.out.println ("The Vector V1 (Used Method SetElementat ()) IS: / N / T" V1); // Set the object of the specified location to the new object v1.removeElement (Integer1); // Vector object V1 Remove the object INTEGER1 Since there is a plurality of Integer1, the first INTEGER1 Enumeration Enum = v1.print is removed from the beginning //. The Vector V1 (Used Method RemoveEleMent ()) IS: "); While (Enum.hasMoreElements ()) System.out.Print (Enum.nexTelement ()" "); system.out.println (); // The method of enumeration (Enumeration) to get each element of the vector object System.out.println ("Top-to-bottom): v1.indexof (integer1)); system.out. Println ("Tottom-to-TOP):" v1.lastIndexof (Integer1)); // Find the position of the object Integer1 in different directions V1.SETSIZE (4); System.out. Println ("The New Vector (Resized The Vector); // Reset V1 size, extra elements are discarded}} Run Results: E: / Java01>

Java Vectorapp The Vector V1 IS: [One, 1, 1, Two, 2, 1, 1] The Vector V1 (Used Method Insert ()) IS: [One, 1, Three, 3.9, 1, Two, 2, 1 , 1] The Vector V1 (Used Method SetElementat ()) IS: [One, 1, Four, 3.9, 1, Two, 2, 1, 1] The Vector V1 (Used Method RemoveEleMent ()) IS: One Four 3.9 1 Two 2 1 1 The position of Object 1 (TOP-TO-BOTTOM): 3 The Position of Object 1 (Tottom-to-top): 7 The New Vector (Resized The Vector) IS: [One, Four, 3.9, 1 ] E: / java01> From the results of the operation of Example 8.3, it can clearly understand the role of the above methods, and there are still some explanations. (1) Class VECTOR defines the number of methods public final int size () this method for obtaining the number of vector elements. Its return value is the number of elements that actually exist, rather than vector. Method cappactly () can be called to obtain a capacity value. Method: Public Final Synchronized Void Setsize (int newsize) This method is used to define the vector size. If the number of existing members of the vector has exceeded the value of Newsize, the excess element of the exceeding is lost. (2) One object enumeration of the Enumeration class is defined in the program is an interface class in java.util, encapsulated a method of enumerating data collection in the enumeration. Method HawMoreElement () is provided in Enumeration to determine that there are other elements and methods NEXTELEMENT () to get the next element. Using these two methods can sequentially obtain the elements in the collection. Method in Vector: Public Final Synchronized Enumeration Elements () This method corresponds to an enumeration type. There are also such methods in other classes in the java.util package to facilitate the user to obtain the corresponding enumeration type. 8.6 Stack The Stack Stack class is a subclass of the VECTOR class. It provides users with the stack of this advanced data structure. The basic characteristics of the stack are advanced. That is, the element is first placed in the stack will be launched. The operation of the stack is provided in the Stack class. Basic Method: Public Object Push (Object Hem) In the HEM press, HEM can be any class of objects. Public Object Pop () pops up an object. Public Object PEEK () Returns the top element, but does not pop up this element. Public int search (Object Obj) Search Object OBJ, returns the location it. Public Boolean Empty () Discriminates whether the stack is empty. Example 8.4 Stackapp.java uses the various methods above. Example 8.4 Stackapp.java.

Import java.lang. *; import java.util. *; public class stackapp {public static void main (string args [) {stack sta = new stack (); sta.push ("apple"); sta.push ( "banana"); sta.push ("cherry"); // Pressing the string object sta.push (new integer (2)); // Pressing the object of Integer, the value is 2 star.push (New float (3.5)); // Pressing the object of float, the value is 3.5 system.out.Println ("The Stack IS," STA); // Corresponding Stack Sta System.out.println ("THE Top of stack is: " sta.peek ()); // Corresponding to top elements, but does not pop up this element in system.out.println (" The position of object cherry is: " sta.search (" cherry " )); // Print the location of the object cherry System.out.print ("Pop the element of the stack:"); while (! Sta.empty ()) system.out.print (sta.pop () ""); System.out.println (); // Popping the elements in the stack and prints. Comparison with the results of the first printed STA, you can see that the stack // advanced feature}} Run Results () 8.7 hash table class HashTable hash table is an important storage method, is also a common Retrieval method. Its basic idea is to calculate the value of the relational code as an argument. By calculating the corresponding function value by a certain function relationship, interpret this value as a node's storage address, deposit the node to the storage address corresponding to the storage address. . The method of retrieving the key code is retrieved. The hash table now has a complete algorithm to insert, delete, and resolve conflicts. In Java, the hash table is used to store objects and enable fast retrieval. Java.util.hashtable provides a way to use a hash table without considering how its hash table really works. There are three construction methods in the hash table, namely: public hashtable () public hashtable (int initialcapacity) Public Hashtable (int initialcapacity, float loadfactor) Parameters INITIALCAPACITY is the initial capacity of HashTable, and its value should be greater than 0. LoadFactor is also known as load factors, is a floating point number between 0.0 to 0.1. It is a percentage, indicating when the hash table needs to expand, for example, there is a hash table, the capacity is 100, and the load factor is 0.9, then the hash table 90% has been used, this hash table It will automatically expand into a larger hash table. If the user does not assign these parameters, the system will automatically process without the need for user worry. HashTable provides basic insertion, retrieval, etc. ■ Insert Public Synchronized Void Put (Object Key, Object Value) Sets a keyword key to the object value and add it to the Hashtable.

If this keyword already exists, the old object corresponding to this key is updated to the new object value. This indicates that the same keyword in the hash table is impossible to correspond to different objects (from the basic idea of ​​the hash table, this is also obvious). ■ Retrieve the public synchronized Object GET (Object Key) Gets the corresponding object based on a given keyword key. Public Synchronized Boolean ContainSkey (Object Key) Declats whether there is a keyword key in the hash table. Public Synchronized Boolean Contains (Object Value) Determines whether the value is an element in the hash table. ■ Delete Public Synchronized Object Remove (Object Key) Deletes the object corresponding to the keyword key from the hash table. Public synchronized void clear () Clears the hash table In addition, Hashtalbe also provides methods to get the corresponding enumeration set: public synchronized enumeration Keys () Returns the keyword corresponding to the keyword. Public synchronized enumeration Elements () Returns the enumeration object corresponding to the element. Example 8.5 HashTable.java gives examples of using havehtable. Example 8.5 Hashtalbe.java.

// Import java.lang. *; import java.util.hashtable; import java.util.Enumeration; public class haashapp {public static void main (string args [) {hashtable hash = new hashtable (2, (float) 0.8 ); // Create a hash table object HASH, the initial capacity is 2, the load factor is 0.8 Hash.Put ("jiangsu", "nanjing"); // give a string object "jiangsu" give a keyword "Nanjing" and joined it to haveh hasput ("beijing", "beijing"); hash.put ("zhejiang", "hangzhou); system.out.println (" the hashtable hash1 is: " Hash ); System.out.println ("the size of this has it is" hash.size ()); // Print Hash content and size enumeration enum1 = hash.eferences (); system.out.print ("the ELEMENT OF Hash IS: "); while (enum1.hasmoreElements ()) System.out.print (Enum1.nexTelement ()"); system.out.println (); // Print content in haveh in Hash IF ( Hash.Containskey ("jiangsu")) System.out.println ("THE CAPATIAL OF JIANGSU IS" Hash.Get ("jiangsu")); hash.remove ("beijing"); // Remove keyword beijing corresponding object System.out.println ("THE HashTable Hash2 IS:" Hash); System.out.Println ("The size of this hash tabl E is " Hash.Size ());}} Run Results: The Hashtable Hash1 IS: {beijing = beijing, zhejiang = Hangzhou, jiangsu = nanjing} The size of this hash Table is 3 the element of hash is: Beijing Hangzhou Nanjing The Capatial of Jangsu Is Nanjing The Hashtable Hash2 IS: {zhejiang = Hangzhou, jiangsu = nanjing} The size of this has 2 HashTable is a subclass of the Dictionary (Dictionary) class. The keyword corresponds to the data value in the dictionary class. The dictionary class is an abstract class. There is also a class Properties in java.util, which is a subclass of HashTable. Use it to perform operations related to object properties. The 8.8-bit Set class BitSet bit collection class encapsulates the operation of a set of binary data. Let's take a look at 8.6 bitsetapp.java.

Example 8.6 BitSetApp.java // Import Java.lang. *; Import Java.util.bitSet; Public Class BitseTApp {Private Static Int N = 5; Public Static Void Main (String [] args) {bitset set1 = new bitset (n ); For (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-108036.html

New Post(0)