Other types of commonly used utility classes

A .StringTokenizer class

1. Description: Without using regular expressions, we can also break down the sequence of characters String object, the object is to use StringTokenizer decomposition.

2. Method configuration:
. 1) StringTokenizer (String s);
This method is configured to a String object analyzer s, default division marks, i.e. the space character, line feed, carriage return, Tab like as a separator character marker. example:

StringTokenizer fenxi=new StringTokenizer("you are welcome");

2) StringTokenizer (s String, String delim);
this method for a String object s configuration analyzer, any sequence of characters arranged in character delim parameter is treated as division marks. E.g:

StringTokenizer fenxi=new StringTokenizer("you#*are*##welcome","#*");
上式中指定字符#和字符*是分隔标记,那么就是#和*的任意排列,
例如###*#*就是一个分隔标记,#也是,*也是

3. Non-constructor
. 1) the nextToken ();
This method may obtain a symbol-by-language character sequence in the String object. Whenever the nextToken call (), will get the next symbol in a language of the character sequence String object, minus the value of a counter variable.

2) as the hasMoreTokens ();
Language Symbol String object acquired character sequence is generally one by one pick-while loop, the control loop, this method can be used, as long as there are language character sequence symbol, the method returns a value of true, otherwise It is false.

. 3) countTokens ();
This method may count variable value obtained analyzer.

Two .Scanner class

1. Description: Scanner object can parse the data required for the program from the character sequence

2. Method:
. 1) the form useDelimiter (regular expression);
This method regular expression as the division marks (the characters that match the regular expression as a separator), without specifying division marks, then the default spaces, tabs back line break as a sequence of characters in the word String object in the resolution.

2) Next ();
Scanner object can call this method returns the sequence of word characters are sequentially parsed.

3) hadNext ();
if the last word has been returned by next () method, then calling this method returns a value of false, otherwise true.

. 4) the nextInt (); nextDouble ();
We can call this method the numeric character conversion return int or double data, if the character is not numeric, Scanner object calls this method InputMismatchException exception occurs, an exception is processed can be called next () method returns a non-numeric character.

Three .StringBuffer class

1. Description: String class with different memory entities StringBuffer class object can automatically change the size, easy to store variable character sequences.

2. Method configuration:
. 1) the StringBuffer ();
This constructor is used to create an object entity may receive initial capacity of 16 characters, when the length of the sequence of characters stored in the entity of the object is greater than 16, the amount of solid content is automatically increased in order to store the added character.

2) the StringBuffer (int size);
initial capacity entity can specify this constructor is assigned to the object size parameter specifies the number of characters, will automatically increase.

. 3) the StringBuffer (String s);
initial capacity entity can specify this constructor object is assigned to the sequence length plus parameter s 16.

3. Non constructor
1) append Method:
StringBuffer the append (String s);
This method String character sequence s object is appended to the current sequence of characters StringBuffer object, the object reference and returns the current StringBuffer.

StringBuffer the append (int n);
this method into n int type String object, then the object's String character sequence appended to the current StringBuffer and returns the current StringBuffer object reference. (Similarly, the same as other data types)

StringBuffer append (Object o);
this method a character sequence object represents the object o StringBuffer appended to the current in the current StringBuffer object and returns a reference.

2) public char the charAt (n-int);
obtained by this method StringBuffer object character sequence on the character position n.

. 3) public void setCharAt (int n, char CH);
this method will replace the character at the current character position n in the sequence of characters specified StringBuffer object parameter CH (n must be meaningful) and returns the current object reference.

. 4) the StringBuffer INSERT (int index, String str);
This method str parameter specified characters into the specified location index parameter, and returns the current object reference.

. 5) public the StringBuffer Reverse ();
This method may be the sequence of characters in the flipped object entity, and returns the current object reference.

. 6) the StringBuffer Delete (int startIndex, int endIndex);
this method to remove a sub-sequence of characters from the character object sequence, and returns (from startIndex ~ endIndex-1).

7) the Delete (int index);
this method is to delete a character index position at.

. 8) the StringBuffer Replace (int startIndex, int endIndex, String STR);
This is the position of the sub endIndex-1 position sequence replaced by startIndex ~ str.

9) StringBuffer object can call length (); method to get the length of the sequence of characters stored in the entity, can call Capacity (); The method of obtaining the actual capacity of the current entity.

Four .Date class and Calendar classes

Description: programming may take the date and time data, and java.util package Calendex Date class and classes, instances of both of said data available for processing.
1.Date class

. 1) a Date ();
This constructor is used to create an object may acquire the current date and time of the machine.

2) a Date (Long Time);
we all know, the computer system of its own time, "AD" is set in January 1970 to 1 at 0:00 (GMT), the parameters of this method to take positive number represents the AD time indicates the time negated BC. 1000 represents 1000ms.

2.Calendar class
. 1) static the getInstance ();
This method can initialize a calendar object.

2) public Final void SET (int year, int month The, int DATE);
public Final void SET (int year, int month The, int DATE, int hour, int minure);
public Final void SET (int year, int month The, int DATE, int hour, minure int, int SECOND);
calendar object can call the method described above any time calendar turn, represents (AD BC when the true parameter negated year)

3) public int GET (Field, int);
This method can get information about the year, month, field of rms hour week and other information, parameters specified by the static constant Calendar, such as: Calendar.get (Calendar.MONTH); return an integer (0 for January in ascending order);

. 4) public Long getTimeInMillis ();
This method may return a Calendar object in milliseconds clocking a current time.

Five .Math class

1. public static Long ABS (Double a);
This method returns the absolute value of a.

2. public static Double max (Double A, Double B);
This method returns the maximum value of a, b of.

3. public static Double min (Double A, Double B);
This method returns the minimum value of a, b of.

4. public static Double Random ();
This method returns a random number (not including 0) between 0 and 1.

5. The public static Double POW (a Double, Double b);
returned by this method is a power of b.

6. The public static Double sqrt (Double a);
This method returns the square root of a

7. The public static Double log (Double a);
This method returns the logarithm of a.

8. The public static Double SiN (Double a);
This method returns the value of a sine

9. The public static Double ASIN (Double a);
This method returns a arcsine

10. The public static double ceil (a double);
This method returns the smallest integer greater than a, and converted to double the integer data type.

11. The public static double Floor (a double);
This method returns the smallest integer smaller than a, and converted to double the integer data type.

12. The public static Long round (Double a);
This method returns ;, i.e. a so-called "rounding" the values (long) Math.floor (a + 0.5 ) a a.

Six .BigInteger class

If the program to be processed particularly large integer, it is an object class BigInteger java.math package can be used.
1. public a BigInteger the Add (a BigInteger val);
returns the current object and val

2. public a BigInteger Subtract (a BigInteger val);
return the difference between the current object and val

3. public a BigInteger Multiply (a BigInteger val);
returns the current object and the product of val

4. public Divide a BigInteger (a BigInteger val);
Returns this object quotient val

5. The public a BigInteger REMAINDER (a BigInteger val);
Returns the current I object of val

6. The public Int the compareTo (a BigInteger val);
returns the current object and the comparison result of val, returns 1, -1, 0, respectively, represent the current object is greater than, less than or equal to val.

7. The public a BigInteger ABS ();
return to the current absolute value of an Integer object.

8. The public POW a BigInteger (int a);
return the current power of a subject.

9. The ; public String toString ()
which returns the current object decimal string.

10. The public String toString (int p);
returns the current object p represents hexadecimal string.

Seven .Random

. 1) public Random ();
This constructor creates a seed as a Random object machine using the current time.

2) public Random (Long seed);
This constructor parameters specified seed seed create a Random object.

. 3) the nextInt ();
if you want to call Random object method returns a random number between 0 ~ m (0 is not included comprising n). Can call nextInt (int m) method (parameters must take positive integer m).

. 4) nextBoolean ();
if need two true and false to true and false boolean value, can call this method.

Eight .Calss class and the Console class

1.class class:
it is a class in the java.lang package, instances of the class program can help create instances of other classes. The most common way is to create an object using the new operator and constructor of the class.
In fact, we can also use the class object to get an instance of a class. method:

. 1) public static class the forName (String className) throws ClassNotFoundException;
This class method returns the associated object class and parameters specified by className. If a class package, package name className must have, for example: className = "java.util.Date".

2) pulbic the newInstance Object () throws an InstantiationException is, Illega (in AccessException);
this method can get an object of class class.

NOTE: use the class object to call newInstance () when instantiating an object class className, className class must Have constructor parameter.

example:

import java.util.Date;
class Rect{
  double width,height,area;
  public double getArea(){
    area = height*width;
    return area;
    }
 }

public class Example{
  public static void main(String args[]){
    try{
      Class cs = Class.forName("Rect");
      Rect rect = (Rect)cs.newInstance();
      rect.width=100;
      rect.height=200;
      System.out.println("rect的面积"+rect.getArea());
      cs = Class.forName("java.util.Date");
      Date date = (Date)cs.newInstance();
      System.out.println(String.format("%tF %<tT %<tA",date));
    }
    catch(Exception e){
      System.out.println(e.toString());
    }
  }
}

2.Console class
if you want to enter a line of text at the keyboard, but do not want to display it on the command line, then you need to use an object in the Console class java.io package to complete.

Usage as follows:
First Console returns an object class using a class called System Console () method.
For example: Console System.console cons = ();
then call readPasswoord () method reads the user to return to a char array keyboard input in a line of text, and the text:
char [] password = cons.readPsaaword ();

Nine .Pattern class and class Matcher

1. What is the pattern matching?
Search and pattern matching is specified sequence of characters.

In java, specifically it provides classes for pattern matching: Pattern Matcher class and classes, two classes located java.util.reges package.
Use examples:

String s = "市话:76.8元,长途:167.38元,短信:12.68元";
String regex = "[0123456789.]+";
Pattern p=Pattern.compile(regex);
Matcher m = p.matcher(s);

Wherein m can be called method objects are:
. 1) public Boolean Find ();
This method may look for the next matching word sequence s regex and, if successful, the method returns true, otherwise returns false.

2) public Boolean The matches ();
This method can determine whether the regex s exact match.

. 3) public Boolean lookingAt ();
This method determines whether there is matching word sequence and regex from the start position s.

. 4) public Boolean Find (int start);
This is the same method of determining a position parameter s from the specified start sequence and if there is matching regex, start parameter is 0, the method and the lookAt () function.

. 5) public String the replaceAll (String replacement);
this method can return a String object, the character sequence of the String object is designated for replacement by the sub-character sequence character sequence s in the pattern regex matching replace all character sequences obtained. (S itself does not change).

. 6) public String ReplaceFirst (String replacement);
This method returns a String object, the object is a sequence of characters specified for the replacement of characters by the character sequence from the first sequence s regex pattern matching with the replacement character sequence to give (note: no change s own).

Published 35 original articles · won praise 0 · Views 1295

Guess you like

Origin blog.csdn.net/c1776167012/article/details/103922628