java base notes (3)

Catch exceptions:

try{

......

}catch(Exception e){

......

}finally{

......

}

Note: you must first big small at the time of writing multiple catch;

 

 

Custom exception:

 

 

 

String String common methods:

String str = "haha xixi pipi"

int i = str.length; (i = 14); returns the length of string str;

int j = str.indexOf ( "xixi"); (j = 5); return "xixi" position in the string str;

String [] arr = str.split ( ""); (arr = [ "haha", "xixi", "pipi"]); a space as delimiter, cutting the string str;

String t = str.substring (5,9); (t = "xixi"); return position of the string between [5,9);

String t1 = str.toLowerCase (); lowercase string;

str.charAt (1); 1 returns the index position of the character;

byte [] b = str.getBytes (); Converts a string to an array of bytes;

Note: char with ''; String with "";

 

StringBuilder and StringBuffer classes: a set of variable content string object, to avoid the newly created string objects frequently have a lot of temporary variables, differences between StringBuilder StringBuffer ratio is more than the realization of thread-safe features, it is more StringBuilder performance high, usually use the StringBuilder preferentially used;

 

Commonly used methods StringBuilder class:

append (parameter): StringBuilder appended to the end of the object;

insert (position parameter): The content into the specified location of the object;

toString (): converts a String object StringBuilder object;

length (): Get string length;

 

Packaging: the basic data types like packaged objects operate;

The method of packaging two categories are provided: providing a data base of this type and other type conversion; string type and present mutual conversion and packaging;

Note: Most of the wrapper class name is the first character capitalized basic data types, such as byte -> Byte; and int wrapper class is Integer, char wrapper class is Character;

  

Unpacking packing: base data type to the object class i.e. packaging containers, packaging into basic data types i.e. unpacking; can be automatic or manual, automatic entry boxes request basic data types and packaging are the corresponding as the corresponding byte byte;

 

Basic data types and the conversion of the string:

Into a string:

int c = 10;

  1. String str1 = String.valueOf(c);
  2. String str2 = Integer.toString(c);
  3. String str3 = c + " " ;

Into basic data types:

String str = "ab"

  1. int a = integer.parseInt(str);
  2. int b = integer.valueOf(str);

 

 

Represents the time: common two categories Date and SimpleDateFormat

Date () returns the current time, the format is

 

 Obviously some common format and the difference in our daily lives, in order to convert it, needed to SimpleDateFormat class, which provides a format () method to convert time format specified format; such as:

Date t = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String date = sdf.format(t)

The date string is displayed at this time is that we have just the time specified format;

Instead, we can convert text string format type Date, then use the parse () method;

Such as:

package com.lzj;
import java.util.*;
import java.text.*;
public class test2 {
    public static void main(String[] args) {
    String day = "2018-11-04 10:30:20";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try{Date date = df.parse(day);
    System.out.println(date);
    }catch(Exception e) {
    }
}
}

operation result:

 

 

 

Operation time, Date class itself with the method of operation time, but it is not recommended, it is recommended to use the Calendar class for processing time and date;

Calendar class is an abstract class in java.util, it initializes itself to get the current time Calendar object via the getInstance (), get the object, and to obtain information values ​​get () method, such as:

Calendar c = Calendar.getInstance();

int year = c.get(Calendar.YEAR);

Wherein Calendar.YEAR class is defined in a static constant;

In addition, Calendar class also provides a number of other commonly used methods:

Date date = c.getTime (); conversion time from the Calendar Date object as the object;

Long time = c.getTimeMillis (); Get the current number of milliseconds;

 

Math class:

Math class java.lang package is located, it is a static method, it is possible to directly have the class name by calling the method, commonly used methods:

double a = 12.885;

  1. long y1 = Math.round (a); return value after rounding;
  2. double y2 = Math.floor (a); returns the largest integer less than the argument;
  3. double y3 = Math.ceil (a); returns the smallest integer greater than the parameter;
  4. double y4 = Math.random (); generating a random number between [0, 1);

 

 

java collections framework

 

 List interface and its implementation class: List element is repeatable order, can precisely control the insertion or deletion of each element; the ArrayList is important to achieve a class of List;

ArrayList class common method to achieve the Add (); elements may be added directly, or specify the location of the additive element: add (index, xxx);

addAll (): increase in a direct array List, such as an array type is not added List, you must undergo Arrays.asList () to convert; such as:

 

 Similarly, it may be selected by specifying parameters inserted location index;

size (): returns the size;

get (index): Returns the element at the location;

iterator (): set into the iterator, and then operated by the iterator Iterator own set of methods, such as through the collection through the iterator. example:

 

 set (index, xxx); the element at the location XXX into;

remove (); delete elements, you can specify the element name directly, you can also specify the index name directly, the same removeAll () method can also delete elements of an array; similarly with addAll () method;

contains (xxx) / containsAll (xxx); determining whether the collection contains xxx xxx small objects or collections, Note: Even if the object properties, methods are the same, but if it is different objects, or will return false;

If you want as long as it is equal to a property returns true, the need to rewrite the equals method in a subject, because of the nature contains () method for each object whether the comparison is equal by the equals method;

Reference template rewritten as follows:

 

 

 

 the indexOf (xxx) method; returns the index position of the object xxx; xxx if there are a plurality of objects returned is the first position index appears, if not, return -1;

 

 

Generics: specifies the type of the collection and its subtype object, a default set various types of data can be increased, compile-time error is not taken out of the element type of type Object, if a generic set, the predetermined the collection can hold only specified types of generics, to store different types of compile-time error will be taken out of the specified type is also sometimes carried out without coercion;

List <Course> coursesToSelect; defining a generic format;

this.coursesToSelect = new ArrayList <Course> (); constructing examples of List format;

 

Note: Generics are not using basic types, use their own packaging or custom class;

 

set interface and its implementation class HashSet;

set of interfaces implemented and the like in the List, but is set not unordered collection of repeat, and so compared List collection method is not set () method, when traversing or only through the foreach iterator, can not get () method, because it is out of order, it is not specified location; set may add an empty object null; contains () method in addition to rewrite equals () method, the need to override hashCode () method, which method returns a hash code, Set HashSet its interface implementation class definition contains () is equal to the need to determine the hash code and the object, will return true, to distinguish it contains () method of the List;

 

 

Map is provided a mapping, by the stored key-value, according to quickly find the key value, not repeat key, value can be a key can map a value;

Map is generic support, such as in the form: Map <K, V>; Method:

put (key, value): Add an object; if the value of an existing key, modify the value of the original value;

remove (key): If specified contains key, the corresponding values ​​are deleted;

keySet (), values ​​(), entrySet (): returns a collection of the Map key, a set of values ​​and a set of values ​​of the key;

Note: entrySet return key to this type represent: Set <Map.Entry <K, Y >> xxx = ?;

HashMap class is a class that implements the Map interface is important, disorderly Entry objects in the HashMap, and key values ​​can be null, but only one key is null (key value can not be repeated)

 containsKey (); determining whether a Key value comprising

containsValue (); Value is determined whether a value comprising;

Note: Again, this method also comprises determining two override equals () method and hashCode () method, or else can only be determined is not the same object;

 

 

Collections class: defines the set of methods for manipulating frame;

Common methods:

Sort (xxx); for xxx rank the set; Note: For the sorting, to ensure the collection of objects implemented Comparable interface, its implementation class to be achieved by the compareTo () method returns a positive number indicates greater than comparison, a negative number less than , equal to 0; and this interface is the comparison rules defined default, it is possible to compare;

When an object is not the object of our Intager the collection of such data type substantially similar packaging, but custom directly sort () method is being given, because does not implement Comparable interface;

In order for our custom objects can use the sort () method, we can object inherits the Comparable interface and override the compareTo () method; by passing an object, compared to the corresponding properties of an attribute of the incoming object and the object itself this ;

Comparator interface: temporary comparison rules defined class; compare implement class () method passing two objects, for comparison;

The implementation class defined Comparator incoming sort (method), and sorted according to the comparison rule this implementation of the class definition:

Such as: studentList is stored Student custom collection of objects, StudentComparator Comparator custom implementation class is class, which is necessary () Compare method comprising;

 

 Note: Comparable with class and the classes are generic Comparator;

Whether it be the Collections class, Comparable or Comparator class category, java collections framework are the classes:

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/lzj-learn/p/11793722.html