1. Timers Timer Class
When constructing a timer, you need to set a time interval and tell the timer what to do when the time interval is reached. The timer needs to know which method to call, and the class that the passed object belongs to implements the ActionListener interface of the Java.awt.event package
Cases:
Implements actionlistener{ publicvoid actionperformed (ActionEvent event) { New Date (); System.out.println (now); New Timeprinter (); Timer t=new timer (listener); T.start ();
2. Local class final variable to do counter
Use an array with a length of 1 to Count
Final int [] Count =newint[1]; for (int i=0;i<dates.length;i++) dates[i]=new Date () { publicint compareTo (Date Other) { counter[0]++; return Super . CompareTo (other); } };
3. Throw the original exception
An exception can be thrown in a catch clause, which is intended to change the type of the exception. If you want to throw an advanced exception without losing the details of the original exception, you can use:
Try { access the database}catch(SqlException e) { =new servletexception ("Database Error"); Se.initcause (e); Throw se;}
Handling of exceptions in 4.finally statements
It is recommended that you use the Try/catch and try/finally statement blocks independently when the statements in finally are unexpected, which can improve the clarity of your code. For example:
InputStream in= ... Try { try{ throw exceptions }finally{ in.close (); }} Catch (IOException e) { Show error dialog}
The inner try statement block has only one responsibility, which is to ensure that the input stream is closed. The outer try statement block also has only one responsibility, which is to ensure that errors are reported. This design method is not only clear, but also has a function, that is, will report the error in finally.
5. Assertions
The assertion mechanism allows you to insert some check statements into your code during testing. Contemporary? release Yes, these inserted detection statements will be automatically removed
Mode one: Assert condition;
Mode two: Assert condition: expression;
The expression is passed into the Assertionerror constructor and converted to a message string.
6. Naming a variable in a generic type
When constructing generics, type variables are used in larger forms and are relatively short. In the Java library, the variable e is used to represent the element type of the collection, and K and V represent the table's keyword and merit type, respectively. T (You can also use adjacent letters U and s when needed) to indicate "any type"
Java generics have a type erase procedure at run time, and the intermediate procedure erases the generic to the object class, so the type parameter cannot be instantiated using the base type. Therefore, no pair<double> only pair<double>
7.Iterator use
The Remove method of the iterator interface removes the element that was returned when the next method was last called. In most cases, it is meaningful to look at this element before deciding to delete an element. However, if you want to delete the element at the specified location, you still need to approximate the element.
Example: Deleting the first element in a string collection:
iterator<string> it = c.iteraotr (); It.next (); It.remove ();
8.Vector and ArrayList
For an experienced Java programmer, a vector class might be used when a dynamic array is required. All methods of the vector class are synchronous. A vector object can be accessed securely by two threads, but if a vector is accessed by a thread, the code spends a significant amount of time on the synchronization operation. While the ArrayList method is not synchronous, it is recommended to use ArrayList instead of vectors when synchronization is not required
9. Thread break Exception handling
Do not suppress interruptedexception anomalies at very low levels, such as
void Mysubtask () { try{sleep (delay);} Catch (Interruptedexception e) {}}
This can cause the caller to be unable to detect or catch this exception. The following two ways should be used
1) call Thread.CurrentThread (). Interrupt () in the catch clause to set the interrupt
Void Mysubtask () { try{ sleep (delay); } Catch (interruptedexception e) { thread.currentthread (). interrupt ();} }
2) A better choice is to mark your method with throws Interrupterexception
void throws interrupterexception{ Sleep (delay);}
10.synchronized keywords
Public synchronized Voi Method () { method body}
Equivalent to
Public void method () { this. Intrinsiclock.lock (); Try { method body }finally{ this. Intrinsiclock.unlock ( ); }}
11. Use Object Lock
public class bank{ public void transfer (int from, int to, int mount) { synchronized -=amount; Accounts[to] +=amount; } System.out.println (...); private double [] Account; private Object lock = new
Collections in the 12.Java class library
ArrayList an index sequence that can grow and shrink dynamically
LinkedList an ordered sequence of how to delete an operation that can be performed efficiently at any location
Arraydeque a double-ended queue implemented with a loop array
HashSet an unordered collection with no repeating elements
TreeSet an ordered set
Enumset a set containing the value of an enumeration type
Linkedhashset a set of elements that can remember the insertion order of an element
Priorityqueue a set that allows for efficient deletion of the smallest element
HASHMAP a data structure for storing key-value associations
TreeMap A mapping table with an orderly arrangement of key values
Enummap A mapping table with key values that are enumerated types
Linkedhashmap A mapping table that remembers the order in which key-value items are added
Weakhashmap A mapping table that can be reclaimed by the garbage collector after its value is useless
Identityhashmap A mapping table that compares key values with = = rather than equals
Reading notes-"Java Core Technology Volume I-Basic knowledge"