Java checks whether the object is null or whether the input set object is null, and determines the length of the array.

Source: Internet
Author: User

Package test; </P> <p> Import Java. AWT. list; <br/> Import Java. lang. reflect. array; <br/> Import Java. lang. reflect. method; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import Java. util. hashtable; <br/> Import Java. util. linkedhashmap; <br/> Import Java. util. linkedhashset; <br/> Import Java. util. using list; <br/> Import Java. util. map; <br/> Import Java. util. set; <br/> Import Java. util. sor Tedmap; <br/> Import Java. util. treemap; <br/> Import Java. util. treeset; <br/> Import Java. util. vector; <br/> Import Java. util. weakhashmap; <br/> Import Java. util. regEx. pattern; <br/> Import Java. util. iterator; </P> <p> public class testnull {<br/> @ suppresswarnings ("unchecked ") </P> <p> Public static void main (string ARGs []) throws exception {<br/> char ch [] [] = new char [10] [10]; <br/> system. out. println ("Char ch [] =" + isnull (CH); </P> <p> byte be [] = new byte [10]; <br/> system. out. println ("Byte be [] =" + isnull (be); </P> <p> float [] Ft = new float [10]; <br/> system. out. println ("float ft [] =" + isnull (FT); </P> <p> double AD [] = new double [10]; <br/> system. out. println ("Double AD [] =" + isnull (AD )); </P> <p> int Ai [] [] [] = new int [10] [10]; <br/> system. out. println ("int Ai [] =" + isnull (AI); </P> <P> Object Ob = NULL; <br/> system. out. println ("Object =" + isnull (OB); </P> <p> string a [] = NULL; <br/> system. out. println ("string a [] =" + isnull (a); </P> <p> List AA = new list (); <br/> system. out. println ("list =" + isnull (AA); </P> <p> arraylist AAA = new arraylist (); <br/> system. out. println ("arraylist =" + isnull (AAA); </P> <p> map = new hashmap (); <br/> system. out. println ("hashmap =" + is NULL (MAP); </P> <p> string A2 [] [] [] [] [] = new string [10] [10] [10] [20]; <br/> system. out. println ("string A2 [] [] [] [] =" + isnull (A2); </P> <p> hashmap MAP2 = new hashmap (); <br/> system. out. println ("hashmap =" + isnull (MAP2); </P> <p> vector keys = new vector (); <br/> system. out. println ("vector =" + isnull (KEYS); </P> <p> hashtable ht = new hashtable (); <br/> system. out. println ("hashtable =" + isnull (HT) ); </P> <p> partition list lT = new partition list (); <br/> system. out. println ("listing list =" + isnull (LT); </P> <p> treeset TT = new treeset (); <br/> system. out. println ("treeset =" + isnull (TT); </P> <p> set Ss = new treeset (); <br/> system. out. println ("treeset =" + isnull (SS); </P> <p> iterator it = new arraylist (). iterator (); <br/> system. out. println ("iterator =" + isnull (IT); </P> <p> export hashmap LLP = ne W linkedhashmap (); <br/> system. out. println ("linkedhashmap =" + isnull (LLP); </P> <p> linkedhashset llT = new linkedhashset (); <br/> system. out. println ("linkedhashset =" + isnull (llT); </P> <p> weakhashmap Wp = new weakhashmap (); <br/> system. out. println ("weakhashmap =" + isnull (WP); </P> <p> string SRA = "'', A, B, C "; <br/> system. out. println (Sra. split (",") [0]); <br/> system. out. println ("SRA =" + isnul L (Sra. split (",") [0]); </P> <p> sortedmap M = new treemap (); <br/> system. out. println ("sortedmap =" + isnull (m )); <br/>}</P> <p>/** <br/> * null check <br> <br/> * @ Param the pinput string to be checked <br> <br/> * @ return Boolean returns the check result, if the input string is null, <br> <br/> */<br/> Public static Boolean isnull (Object pinput) is returned) {<br/> // determines whether the parameter is null or ''<br/> If (pinput = NULL | "". equals (pinput) {<br/> return true; <br />} Else if ("Java. lang. string ". equals (pinput. getclass (). getname ())) {<br/> // determine the string type of the input parameter </P> <p> // replace various spaces <br/> string tmpinput = pattern. compile ("// R | // n | // u3000") <br/>. matcher (string) pinput ). replaceall (""); <br/> // match null <br/> return pattern. compile ("^ (// s) * $") <br/>. matcher (tmpinput ). matches (); <br/>}else {<br/> // method class <br/> method = NULL; <br/> string newinput = ""; <br/> Try {<br/> // access the size method of the input parameter <br/> method = pinput. getclass (). getmethod ("size"); <br/> // determines the size </P> <p> // converts it to the string type <br/> newinput = string. valueof (method. invoke (pinput); <br/> // when the size is 0 <br/> If (integer. parseint (newinput) = 0) {</P> <p> return true; <br/>}else {</P> <p> return false; <br/>}< br/>} catch (exception E) {<br/> // Access failed <br/> try {<br/> // access the getitemcount method of the input parameter <br/> Method = pinput. getclass (). getmethod ("getitemcount"); <br/> // determines the size </P> <p> // converts it to the string type <br/> newinput = string. valueof (method. invoke (pinput); </P> <p> // Where getitemcount is 0 <br/> If (integer. parseint (newinput) = 0) {</P> <p> return true; <br/>}else {</P> <p> return false; <br/>}< br/>} catch (exception ex) {<br/> // Access failed <br/> try {<br/> // determine the length of the input parameter </P> <p> // when the length is 0 <br/> If (array. getl Ength (pinput) = 0) {</P> <p> return true; <br/>}else {</P> <p> return false; <br/>}< br/>} catch (exception Exx) {<br/> // Access failed <br/> try {<br/> // access the hasnext method of the input parameter <br/> method = iterator. class. getmethod ("hasnext"); <br/> // convert string type <br/> newinput = string. valueof (method. invoke (pinput); </P> <p> // convert the hasnext value <br/> If (! Boolean. valueof (newinput) {<br/> return true; <br/>} else {<br/> return false; <br/>}</P> <p>} catch (exception exxx) {<br/> // The preceding conditions do not meet </P> <p> return false; <br/>}< br/>

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.