Java Foundation Chapter 12th (Exception handling, tools, collections)

Source: Internet
Author: User

First, exception handling

1. Two types of anomalies:

(1) Programmer's own problem (runtime exception)

(2) External problems (controllable anomalies)

2. Detailed description of two types of anomalies

(1) Run-time exception

The program interrupts the program when a run-time exception occurs.

Example: P1 as the object;

P1 p = new P1 ();

p = null;

P.sayhello ();

This will be reported null pointer exception NullPointerException ();

The above statement is equivalent to

throw new NullPointerException ();

Types of Run-time exceptions:

throw new NullPointerException (); Null pointer exception

throw new ArrayIndexOutOfBoundsException (); Array out of bounds exception

throw new Arrithmeticexception (); Operation exception

throw new ClassCastException (); Type Conversion exception

throw new SqlException (); Exception to database operation

(2) Controllable anomalies

When an exception occurs, the program is not interrupted.

To throw an exception in the format:

try{

There can be multiple exceptions, but if it is not a catch (Exception e), it is necessary to write multiple catch

}catch (Exception) {

}finally{

The finally executes regardless of the occurrence of an exception in the program.

}

Example: try{

Class.forName ("Com.mysql.jdbc.Driver");

Connection conn = drivermanager.getconnection ("abc");

}catch (Exception e) {

E.printstacktrace ();

}finally{

System.out.println ("haha");

}

3. Exception Tree

(1) The difference between throw and throws:

Throw: Used in code block, must throw an exception, meaningless.

Throws: Used after the method.

(2) Example:

Class p1{

public void Conndatabase () throws exception{

Class.forName ("Com.mysql.jdbc.Driver");

Connection conn = drivermanager.getconnection ("jdbc:mysql://localhost:3306");

}

}

public class main{

public static void Main (string[] args) {

P1 p = new P1 ();

try{

P.conndatabase ();

}catch (Exception e) {

E.printstacktrace ();

}

}

}

Second, the tool class

1.Math function

(1) ABS (); Absolute

Example: Math.Abs (-1); 1

(2) round (); Rounded

Example: Long a = Math.Round (8.78); 9

Integer B = Math.Round (8.78f); 9

(3) ceil (); Rounding up

(4) floor (); Rounding down

(5) Pow (,); Ask the Square

Example: Double a = POW (2.0, 3.0); 8

(6) sqrt (); Open Square

(7) random (); Random number [0,1]

2.Date function

(1) Example: Date d = new Date ();

SimpleDateFormat fm = new SimpleDateFormat ("Yyyy-mm-dd");

(2) Calendar class

Calendar C = calendar.getinstance ();

Set () Method:

C.set (calendar.year,2017);

C.set (calendar.month,0);

Get () Method:

int date = C.get (calendar.year);

int date1 = C.get (calendar.date);

GetTime () Method:

Get the current time

Example: Date d = c.gettime ();

SimpleDateFormat fm = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

String m = Fm.format (d);

Three, the collection

You can put different data types and grow longer.

List (parent Class)--arraylist (subclass), LinkedList (subclass)

1.ArrayList

(1) Used to iterate frequently, find the block in the program.

(2) The Order of placement is the order of storage, allowing the same object to be placed.

(3) Format:arraylist< type > list = new arraylist< type > ();

(4) Method:

Add (); Add method

Add (location, content); Insert Content

AddAll (); Merge another object

Remove (); Delete

Get (); Get content

(5) Example:

ArrayList list = new ArrayList ();

List.add (20);

List.add ("Hello World");

2.LinkList

Format:linkedlist< type > list = new linkedlist< type > ();

(1) Do a lot of add, delete operations.

(2) The Order of placement is the order of storage, you can put the same object.

(3) The method is the same as ArrayList, but more than

AddFirst (); Insert at the beginning

AddLast (); Insert at end

3. Iterating through the collection

(1) For loop traversal

Example: for (int i = 0; i < list ... Size (); i++) {

String ob = List.get (i);

SYSTEM.OUT.PRINTLN (OB);

}

(2) Iterative traversal

Example: Iterator iter = List.iterator ();

while (Iter.hasnext ()) {

String ob = Iter.next ();

SYSTEM.OUT.PRINTLN (OB);

}

(3) for each traversal

Example: for (String ob:list) {

SYSTEM.OUT.PRINTLN (OB);

}

Set (parent Class)--hashset (subclass), TreeSet (subclass)

1.HashSet

(1) The Order of storage, not the order of placement.

(2) cannot put the same object.

(3) Format:hashset< type > Set = new hashset< type > ();

(4) Method: No Add (location, content);

No get ();

The rest is the same as arraylist,linkedlist.

(5) Traversal: No for () traversal, rest with arraylist,linkedlist.

2.TreeSet

(1) Dictionary sequential Storage (ascending).

(2) cannot put the same object.

(3) Format:treeset< type > Set = new treeset< type > ();

(4) Method: with HashSet.

(5) Traversal: with HashSet.

TreeSet setting sorting

Write a rule, create a class, add an interface compare< object, and write rules.

Example: compare C = new compare ();

TreeSet set = new TreeSet (c);

Write to determine whether the same object rules

Override Hashcode (), Equals () method

Steps:

class, right-click source--> override/......--> equals ()/hashcode ()

Hashcode () {

return 1;

}

Equals () {

if (judgment rule) {

return true;

}

}

Java Foundation Chapter 12th (Exception handling, tools, collections)

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.