Java Learning notes (14)

Source: Internet
Author: User

A, hash list

1. Capacity: Hash array size in hash table

2. Hashing operation:key-> Hash value algorithm

3. Hash barrels: "linear set" of elements with the same hash value

4. Load factor: Is the hash array load rate, that is, the number of elements/hash array size

5. Hash Lookup: Calculates the hash value based on key, finds the hash bucket, compares the key in the hash bucket sequentially, returns the value if the same

6. Hash list key is different, value can be repeated

Example:

<span style= "White-space:pre" ></span>user u1=new User (1, "Tom", "123"); User U2=new User (2, "Jerry", "123"); User U3=new User (3, "John", "123"); User U4=new User (4, "Robin", "123"); User U5=new User (5, "GG", "123"); User U6=new User (6, "MM", "123"); User U7=new User (7, "DD", "123"); User[] us={u1,u2,u3,u4,u5,u6,u7};//loginstring name= "MM"; String pwd= "123"; User u=null;for (int i=0;i<us.length;i++) {if (Us[i].getname (). Equals (name)) {u=us[i];}}

Second, abnormal

1. Unexpected results of behavior

2. If an exception is thrown by a method, the method must declare the exception thrown

Exception declaration: An unexpected result of declaring a method on a method, such as:

User Reg (String name,string pwd,string email) throws userexistexception;

User Login (String name,string pwd) throws nameorpwdexception;

3. Exception classes generally inherit from exception

4. Calling the method that throws the exception must handle the exception

4.1 Call try,catch,finally Capture

4.2 Direct re-throw exception

4.3 Capture, then throw

Processing depends on the specific logic

5. If the code has an exception, the code after the exception will no longer execute

6.try,catch,finally

Try is an attempt to run a code block, and if an exception is caught by a subsequent catch, the code no longer executes after the exception occurs

The catch code block is the exception handling code. Need to provide reasonable handling, exception handling is related to the specific business logic, you can write multiple catch to handle a series of exceptions, but note: the size of the exception, large types of put to the back processing. Sometimes catch (Exception) coarse-grained processing exception, code concise semantic ambiguity, according to business logic appropriate selection

Finally blocks of code, regardless of whether or not an exception occurs, are often used to handle on-site cleanup, such as: Reliable Data path connection shutdown

7. The exception has a basic principle: to be able to deal with the underlying processing, but if not processed, must be thrown into the caller (method). should not simply abandon

8. Exception capture re-throw, is a kind of the underlying exception is encapsulated, converted to another type of exception

9. It is recommended to use E.printstacktrace () when catching anomalies;

10. Classification of exceptions

Throwable

Error is a system unrecoverable fault that occurs by the JVM

OutOfMemoryError Heap Memory Overflow

Stackoverflowerror Stack Memory Overflow

Exception program can check for handled exceptions

Java.text.parseException format when parsing an object

RuntimeException Non-check exceptions, Javac ignores syntax checks for such exceptions, such as: Exception throws

IllegalArgumentException

NullPointerException

ArrayIndexOutOfBoundsException

ClassCastException

NumberFormatException

11. A large number of custom classes are used in the software, typically inherited from exception

The exception class naming should have the practical meaning

Example:

public class Exceptiondemo {User U1=new User (1, "Tom", "123"); User U2=new User (2, "Jerry", "123"); User U3=new User (3, "John", "123"); User U4=new User (4, "Robin", "123"); User U5=new User (5, "GG", "123"); User U6=new User (6, "MM", "123"); User U7=new User (7, "DD", "123"); User[] Us={u1,u2,u3,u4,u5,u6,u7}; List<user> users=arrays.aslist (US);p ublic static void Main (string[] args) {Exceptiondemo demo=new Exceptiondemo ( ); Try{user u=demo.findbyname ("mm");//user u=demo.findbyname ("MMM");//user U0=DEMO.FINDBYNAME0 ("MMM"); System.out.println (U.getname ()); U=demo.login ("abc", "123");} catch (Usernotfoundexception e) {//Exception capture E.printstacktrace (); System.out.println (E.getmessage ());//exception handling}catch (Nameorpasswordexception e) {e.printstacktrace ();}} Public User login2 (String name,string pwd) throws Passwordexception,usernotfoundexception{user U=findbyname (name);// The exception is thrown directly again if (U.getpwd (). Equals (pwd)) {return u;} throw new Passwordexception ("PWD"); Public User Login (String name,string pwd)//exception capture wrapper again throws throws Nameorpasswordexception{trY{user u=findbyname (name); if (U.getpwd (). Equals (pwd)) {return u;} throw new Nameorpasswordexception ("pwd Wrong");} catch (Usernotfoundexception e) {throw new Nameorpasswordexception (e);}} Public User findbyname (String name) throws usernotfoundexception{//exception declaration for (int i=0;i<us.length;i++) {User u=us[i]; if (U.getname (). Equals (name)) {return u;}} throw new Usernotfoundexception ("no");//exception Handling}public user FINDBYNAME0 (String name)//traversal using iterators throws Usernotfoundexception{iterator<user> Ite=users.iterator (); User U0;while (Ite.hasnext ()) {u0=ite.next (); if (U0.getname (). Equals (name) {return u0;}} throw new Usernotfoundexception ("No Yes 0");}}

Third, IO

1.java.io

2.File class, representing files, folders (directories)

A. View file/directory properties, directory is relative directory is JAVAVM startup directory, eclipse default is relative directory

B. Creating a File/directory

C. Access file system (Directory system), column file/directory/filter Column directory

Example:

public static void Main (string[] args) {file Bin=new file ("Bin"); System.out.println (Bin.getabsolutepath ()); System.out.println (Bin.isdirectory ()); System.out.println (Bin.canread ()); Long time=bin.lastmodified ();//system.out.println (Dateformatutils.format (Time , "YYYY-MM-DD")); SimpleDateFormat fmt=new SimpleDateFormat ("Yyyy-mm-dd"); System.out.println (Fmt.format (new Date)), long size=bin.length ();//file property, filename file=new ("src/day16/ Filedemo.java "),//file file=new File (" Src\\day16\\filedemo.java "),//Two kinds of/\,java are supported, but \ to remember the translation Oh System.out.println ( File.isdirectory ()); System.out.println (File.isfile ());//Manage files, directories, create, rename, delete files or folders file Dir=new ("demo"); System.out.println (Dir.exists ());//falseif (!dir.exists ()) {<span style= "White-space:pre" ></span>// If the file or directory does not exist, create dir.mkdir ();//dir.createnewfile ();//touch}system.out.println (Dir.exists ());//truedir.delete (); <span style= "white-space:pre" ></span>//delete file or directory//dir.renameto (dest) renamed System.out.println (Dir.exists () );//falsefile test=new FilE ("test");d Ir.renameto (test), <span style= "white-space:pre" ></span>//file or directory rename//access filesystem file Work=new File ("."); /current directory, that is, project folder string[] List=work.list ();//The same as the Linux ls command System.out.println ("Content in" +<span style= "White-space :p Re "></span>//displays relative path Work.getabsolutepath ()); for (int i=0;i<list.length;i++) {String name=list[i]; SYSTEM.OUT.PRINTLN (name);}}
Example: Displaying all the files in a large folder containing a directory
public class Listalldemo {public static void main (string[] args) {file src=new file ("src"); list (SRC, ". Java");// Lists all sub-files in src}/** * Displays files of the specified type for the specified folder * Recursive processing sub-file * @param dir * @param ext */public static void list (file dir,string ext) {//Display All files in the current directory System.out.print ("-----------------"); System.out.println (Dir.getabsolutepath ()); System.out.print ("-----------------"); ListFile (dir,ext);//Get all subdirectories of current directory file[] All=dir.listfiles (); for (int i=0;i <all.length;i++) {File d=all[i];if (d.isdirectory ()) {list (d,ext);}} Traverse subdirectories, listing files for each subdirectory}public static void ListFile (file dir,string ext) {file[] All=dir.listfiles (new Filter (EXT)); for (int i=0;i<all.length;i++) {File f=all[i]; System.out.println (F.getname ());}} Static class Filter implements Filenamefilter{<span style= "White-space:pre" ></span>//implements FilenameFilter interface , to implement the filtering of the file private String ext;<span style= "White-space:pre" ></span>//add a property, so that the file type to be filtered is not written dead, more flexible public Filter (String ext) {this.ext=ext;} Public Boolean accept (File dir,string name) {REturn Name.endswith (EXT);}} 


Java Learning notes (14)

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.