java-- two-dimensional array as well as exceptions and exception handling

Two-dimensional array

Nested array is an array of two-dimensional arrays

  • Declared two-dimensional array of
    data type [] [] array name;
    data type array name [] [];
  • Initialization:
    Dynamic Initialization:
    Data Type [] [] = new Array name Data type [a length dimension] [dimensional length];
    small array of the same length dimension of each of the second
    type of data [] [] = new array name data type [a length dimension] [];
    length of each small array may be different from the second dimension, the second dimension is small arrays has not been created
    for each array arr [outer array index] = new data type [length ] | ... way to create a one-dimensional array
  • Static initialization:
    Data Type [] [] = new Array name Data type [] [] {{1,2}, {1}, {1,2,3} ...};
    Data Type [] [] array name = { {1,2}, {1}, {1,2,3} ...};
  • Traversing the two-dimensional array:
    dual nested for loop

Arrays array operation tools

  • Jdk class learning ways:
    1. The class is doing, what role
    2. constructors
    3. function (method)
    jdk the API: instruction manual, can effectively help us learn java in the class.
  • The method of operation of common array
    1.static void sort (int [] a ); the specified int type array into ascending numerical order.
    2.static String toString (int [] a ); Returns a string representation of the contents of the array.
    3.static int binarySearch (int [] a , int key); using the binary search method to search the specified int array to the specified value.
    4.static void fill (boolean [] a , boolean val); boolean value assigned to specify each element of the specified array of boolean.
    5.static void fill (boolean [] a , int fromIndex, int toIndex, boolean val); specified boolean value assigned to each element of the specified range of the specified array of boolean. fail to toIndex position.
    6.static int [] copyOf (int [ ] original, int newLength); Copies the specified array, truncating or padding with zeros (if necessary), so that the copy has the specified length.
    7.static int [] copyOfRange (int [ ] original, int from, int to); to the end position is not included.
    8.static boolean equals (double [] a , double [] a2); if the two specified double array equal to one another, it returns true.
    9.static boolean deepEquals (Object [] a1 , Object [] a2); if the two specified arrays are deeply equal to each other, it returns true.
    10.static String deepToString (Object [] a); Returns the specified array "deep contents" string representation.

abnormal

Exception: Program sick
             Throwable
             / 1 \ 2
            Error Exception
                / 1 \ 2
        CheckedException RuntimeException

  • Error: this type of error is usually a virtual machine production, the programmer can not control, no tube

  • Exception:
      Abnormal check | abnormal CheckedException compile time: If you encounter must be processed, otherwise the program can not run, occurs at compile time.
      RuntimeException abnormal Runtime: running before we know whether there is an exception, robust processing enhancement program (if ... else).

  • Some common abnormal operation:
    1. The null pointer exception a NullPointerException
    2. bounds array subscript An ArrayIndexOutOfBoundsException
    3. negative abnormal a NegativeArraySizeException
    4. Mathematical abnormal an ArithmeticException
    5. The string index bounds exception StringIndexOutOfBoundsException

throw: Manufacturing exception
  if an exception is thrown required in the program itself, use throw statement, throw statement may be used alone, throw statement is not thrown exception class, but an exception instance, and each instance can throw an exception .

  • throw statement syntax is as follows:
    throw a ClassNotFoundException new new ();
  • Exception handling methods:
    1. throws an exception: throws an exception thrown on the floor
    2. catch exceptions:
   	try{
  		可能会出现异常的代码
 	}catch(ClassNotFoundException e){ //= new ClassNotFoundException();
  		如果出现ClassNotFoundException类型的异常,执行这里的代码....
  	}catch(NullPointerException e){
  		如果出现NullPointerException异常,执行这里的代码
  	}...
 	 catch(Exception e){
 		接收除了以上的其他的异常,执行这里的代码
  	}finally{
  		无论try中的代码是否出现异常,finally中这里的内容一定会执行
  	}
  • Note:
    1.try later can be connected to a plurality of catch, catch different types of exceptions.
    2. Write a wide range of catch in the end, small-scale EDITORIAL otherwise never be reached.
    3. If the try in the event of abnormality, the code behind the try will not be executed, the execution code corresponding to the catch.
    4. If there is a method of rewriting, rewriting subclass exception thrown <= parent class thrown.

Custom exception:
   in addition to the exception class java provided to be custom exception.
Learning abnormalities:

  1. Learn what abnormal classification, their own characteristics.
  2. *** exception handling
    exception when compiling: 1) throws 2) try ... catch
    run-time exceptions: 1) enhancement procedures robustness 2) throws 3) try ... catch
  3. Custom exception to use.

Guess you like

Origin blog.csdn.net/qq_41899248/article/details/91345700