A method that checks whether an array contains a value uses Listpublicstaticbooleanreturn arrays.aslist (arr). Contains (Targetvalue); } Use Setpublicstaticsetnew hashset<string> (arrays.aslist (arr)); Returnset.contains (Targetvalue);} Use cycle to judge publicstaticbooleanforifreturntrue; } Returnfalse; } using Arrays.binarysearch () publicstaticbooleanint a = Arrays.binarysearch (arr, targetvalue); If0returntrue; elsereturnfalse;} Time complexity publicstaticvoidnew "CD" "BC" "EF" "DE" "AB"}; Use Listlong StartTime = System.nanotime (); Forint0100000; i++) {uselist (arr,);} Long endTime = System.nanotime (); Long Duration = Endtime-starttime; System.out "Uselist:" 1000000); Use Set startTime = System.nanotime (); Forint0100000; i++) {Useset (arr,);} endTime = System.nanotime (); Duration = Endtime-starttime; System.out "Useset:" 1000000); Use loop StartTime = System.nanotime (); Forint0100000; i++) {Useloop (arr,);} endTime = System.nanotime (); Duration = Endtime-starttime; System.out "Useloop:" 1000000); Use Arrays.binarysearch () StartTime = System.nanotime (); Forint0100000; i++) {Usearraysbinarysearch (arr,);} endTime = System.nanotime (); Duration = Endtime-starttime; System.out "Usearraybinary:" 1000000);} :::: Using an array of length 1k new1000]; Random s = new Random (); Forint0i++) {Arr[i] = string.valueof (S.nextint ());} :::: Using an array of length 10k new10000]; Random s = new Random (); Forint010000; i++) {Arr[i] = string.valueof (S.nextint ());} :::: Summaryarrays.binarysearch () use arrayutils arrayutilscontainsimport org.apache.commons.lang3.ArrayUtils; publicstaticbooleanreturn Arrayutils.contains (Arr,targetvalue); }
method to check if an array contains a value