Java Basics (eight)

Source: Internet
Author: User
Tags throw exception throwable

(a) HashMap
(1) The HashMap collection itself is based on a hash table

It guarantees the uniqueness of the keys (map is valid for keys)
Hashmap<string,string>

  • String: ID number of the person
  • String: Name of person
    Hashmap<integer,string>

  • Integer: Age
  • String: Name

    Hashmap<string,student>

  • String: School Number
  • Student: School number corresponds to one student: name and age
    Hashmap<student,string> key: is a custom type
    Student: Student: Age and name
    String: Dynasty
  • If the object has the same value as the member variable, the same person is considered.
    *hashmap Bottom Hash table
    Hash table: Dependent on two methods hashcode (), Equals () method
    (2) linkedhashmap<k,v>
    LINKEDHASHMAP<K,V>: is a map interface implemented based on a hash table and a list of links

    哈希表保证键的唯一性链接列表保证元素有序性(存储和取出一致)

    (3)
    TREEMAP implementation of MAP interface based on red-black tree structure

    (4) Thread-Safe classes:

  • StringBuffer: String buffer
  • Vector:list Collection
  • Hashtable:map Collection of
    (5) Collection
    The difference between Collectio and collections:
  • Collection: The root interface of the top-level single-column collection, which is a collection that is an interface
  • Collections: is a tool class for collection operations, with some features: random substitution, binary lookup within a collection, reversing the elements of a collection
  • * Two-point search for collections

  • public static <T> int BinarySearch (list<t> List, T key)
  • Static T Max (Collection coll): Gets the maximum value in the collection
  • public static void reverse (list<?> list): Reverses the order of elements in the collection
  • public static void Shuffle (list<?> list): Disrupts elements in the collection
  • public static void sort (list<t> List)
    (ii) Exception
    (1) When the program has some problems, it can be a serious problem, can be an exception, these are usually throwable
    Throwable class is a superclass of all errors or exceptions in the Java language
  • Throwable
  • Error
  • exception
  • Example:
    Zhang San a trip to the mountains
    1) Zhang San Bike to travel, Mountain Road collapsed, unable to move forward-----> Force Majeure factors
    2) Zhang San Bike to travel, riding to find the tires are not gas-------> need to check the problem
    3) riding, the mountain road has gravel, he had to ride on the stone, resulting in a flat tire----> own cause, no Zuo no die

    Non-Resistance factors----->error: A Serious problem memory overflow (when loading a large number of images in the page, the program will hang---use a third-party framework to handle: Imageloader)

  • Abnormal:
  • Compile-time Exceptions: As long as there is not a run-time exception, the text format, collectively known as the compile-time date---parsing java.util.Date type: parseexception: Parsing exception
  • Compile-time Exceptions: developers must deal with them!
  • Run-time exception: RuntimeException
  • The compilation passed, but the developer code is not rigorous (Nullpointerexceptino, etc...)
    (2) Exception handling is divided into two types:
    1) try...catch...finally (standard format): Catch exception
    2) throws ... Throw exception

    Morph format ...
    Try...catch ...
    Try...catch...catch ...
    Catch....

    try{
    Some code
    The less code in the try, the better.
    The code contains the code that may be having problems
    }catch (Exception class exception class object) {
    Try has an exception, the exception described is just the catch exception class, will execute the catch inside the code
    Handling Exceptions
    }
    (3) How to handle multiple exceptions
    What happens when you have two or more two exceptions?

    try{
    Code that may be having problems
    }catch (Exception class object name) {
    Code to handle exceptions
    }

    try{
    Code that may be having problems
    Int[] arr = {A-i};
    Sop (Arr[3]);
    int a = 10;
    int b = 0;
    Sop (A/b);

    }catch (Exception class name 1 Object name 1) {
    Exception handling
    }catch (Exception class Name 2 object Name 2 () {

    //异常处理

    }
    Dk7 a different way to handle multiple exceptions later
    try{

            可能出现问题的代码;

    }catch (Exception class Name 1 | Exception class Name 2 | ... Object name) {

    处理异常

    }
    (4) What is the difference between a compile-time exception and a run-time anomaly?

    Compile time: The developer must perform display processing, do not process, compile cannot pass,
    Run-time exception: No display processing is required and can be processed as the compile time

    Second way to handle exceptions: Throw an exception with throws (following the method)
    XXX return value method name () throws exception class name {
    }
    Standard format try{...} catch (Exception class object) {//exception handling} Execute code inside try
    , if a problem occurs, it creates an exception object through the code's problem, and then the exception class is consistent through the exception object and the catch
    If there is a consistent situation, the code inside the catch will appear, executing the method inside the Throwable

  • public string GetMessage (): Message string
  • Public String toString (): Brief description of the exception ":
  • ": Consists of a colon and a space

  • public void Printstacktrace (): The return value void is called directly, contains the message string, and ":"
  • Information description, the specific occurrence of abnormal code positioning and positioning of the source on the
    (5) Throw: Represents an exception that throws an exception object (throw new Exception class name (): How anonymous objects are)
    Try...catch...finally
  • The finally statement body is bound to execute unless the JVM exits.
    (6) Custom exception classes, two ways
  • 1) Customize a class that inherits from exception
  • 2) Inherit RuntimeException
    Considerations in Exceptions:
    Considerations when subclasses inherit from a parent class
  • 1) When a subclass overrides a method of a parent class, the exception that the subclass's method throws must be either a method exception for the parent class or a subclass of the parent method exception
  • 2) When the subclass overrides the parent class method, if this method in the parent class does not throw an exception, then the subclass overrides this method can not throw an exception, only Try...catch
    Three File class
    (1) File class: An abstract representation of the path to a file or directory. The IO stream is the operation of the file.
  • Public File (String pathname): Represents the form of an abstract path representation of pathname (used in this way in development)
  • Public File (String parent,string Child)
    Creates a new File instance based on the parent pathname string and the child pathname string
    Public file (file parent, String Child): Constructs a file instance based on a Document object and a path of files
    (2) features related to creation:
  • public boolean CreateNewFile (): Creates a file that is created under this abstract path when the file does not exist
  • public boolean mkdir (): Creates a folder if the folder exists and does not create
  • public boolean mkdirs (): Creates a folder, if the parent directory does not exist, creates this directory
    (3) Delete function
  • public boolean Delete (): Delete files or folders (directories cannot be empty): Delete files or directories individually

    If I create a file/directory, where does it come from without a write letter?
    is created under the current project path
    (4) Absolute path: c:\xxx\xxx.txt
    Relative path: a.txt

    Recall: HTML page (later JSP page)
    Background address: URI Uniform Resource Locator

    <form action="http://localhost:8080/Web工程名称/后台(XXX.servlet)"  metod="提交方式">        用户名:<input type="text" name="username"/>        密码:<input type ="password" name="pwd"/>            <input type ="hidden" name="hid"/>        </form>

    Redirect/forward of page
    Response.sendredirect ("/Project name/xxx.jsp");

    Rename function of File:

  • The public boolean renameto (file dest) renames the file represented by this abstract path name.
    If the path name is consistent, then just rename
    If the two-time path names are inconsistent, then not just rename, and cut
    (5) The judgment function of the file class
  • public boolean isdirectory (): Determine if it is a folder
  • public boolean isfile (): Determines whether the file is
  • public boolean canRead (): Readable
  • public boolean Canwriter (): Writable
  • public Boolean exists (): Presence
  • public boolean Ishidden (): Whether it is a hidden file
    (6) The FETCH function of the file class:
  • Public String GetAbsolutePath (): Gets the absolute path of the abstract file
  • public string GetPath (): Gets the string relative to the path
  • Public String GetName () returns the name of the file or directory represented by this abstract path name
  • public long Length () returns the length of the file represented by this abstract path name.
  • Public long LastModified (): The last time the file was modified (time millisecond value)
    (7) Advanced features of file
  • Public string[] List (): Gets a string array of all the folders and file names under the current path
  • Public file[] Listfiles (): Gets all folders under the current path and file array of files
    (iv) IO stream
    (1) IO: A data transfer between the device and the device!
    Classification of IO streams:
    According to the direction of the flow:
  • Input stream: Read file (e:\a.txt): Output The contents of this file after reading from a file on the hard disk
  • Output stream: Write file: Read E:\a.txt content---> Write to F-disk
    By type of data:
    BYTE stream
  • BYTE input stream: InputStream: Read bytes
  • BYTE output stream: OutputStream: Write section
    Character Stream
  • Character input stream: Reader: Read character
  • Character output stream: Writer: Write character
    (2) FileOutputStream
    On the method of byte output stream write data
  • public void Write (int b): Write one byte at a time
  • public void Write (byte[] b): Write one byte array at a time
  • public void Write (byte[] b, int off,int len): Writes part of a byte array at a time
    Question: How do I append data at the end?
  • Public fileoutputstream (File file, Boolean append): Specifies true to append data at the end
    (3) FileInputStream
    Construction method: Public FileInputStream (String name) reads data:
  • public abstract int Read (): reads one byte at a time
  • public int read (byte[] b): reads one byte array at a time (reads the actual number of bytes)
    (v) Recursion
    Recursion: Only is a phenomenon of method invocation method itself
    Math class: Max () method Math.max (10,math.max (15,20)); Method nesting

    Example
    Method recursion:

    public void show(int n){    if(n<0){        System.exit(0) ;//jvm退出了    }    System.out.println(n) ;    show(n--) ;}

    Three prerequisites for recursive methods:

  • 1) method recursion must have a condition
  • 2) Some rules must be met
  • 3) Be sure to write a method
  • * Note: The constructor method does not exist for method recursion
    Example
    There used to be a mountain, there was a temple in the mountain, there was an old monk and a little monk in the temple, the old Monk told the Old Monk a story is:
    There used to be a mountain, there was a temple in the mountain, there was an old monk and a little monk in the temple, the old Monk told the Old Monk a story is:
    There used to be a mountain, there was a temple in the mountain, there was an old monk and a little monk in the temple, the old Monk told the Old Monk a story is:
    There used to be a mountain, there was a temple in the mountain, there was an old monk and a little monk in the temple, the old Monk told the Old Monk a story is:
    The end of the condition: The mountain collapsed, or the monk hung up

Java Basics (eight)

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.