1. query Arrays: binarysearchstring [] strs1 = new string [] {"1", "2", "3", "4"}; int I = arrays. binarysearch (strs1, "2"); system. out. println (I); 2. array overwrite: fillstring [] strs1 = new string [] {"1", "2", "3", "4"}; arrays. fill (strs1, 2, 3, "Haha"); For (INT I = 0; I <strs1.length; I ++) {system. out. println (strs1 [I]);} note: the first parameter is an array, and the last parameter is the new value after overwriting. The overwrite position is, starting from the second parameter, the number of overwrite array items is, the third parameter minus the second parameter. 3. sort Arrays: sortstring [] strs1 = new string [] {"1", "2", "6", "4"}; arrays. sort (strs1); For (INT I = 0; I <strs1.length; I ++) {system. out. println (strs1 [I]);} sixth, the conversion of data and other data types we often need to convert the array to list, arraylist, and other data types, then we need to convert them back. Convert the array to list: String [] strs1 = new string [] {"1", "2", "6", "4"}; List list = arrays. aslist (strs1); convert the array to arraylist: String [] strs1 = new string [] {"1", "2", "6", "4 "}; list list = arrays. aslist (strs1); arraylist Al = new arraylist (); Al. addall (list); Convert list to array: String [] strs1 = new string [] {"1", "2", "6", "4 "}; list list = arrays. aslist (strs1); string [] strs2 = (string []) list. toarray (New String [0]); For (INT I = 0; I <Strs2.length; I ++) {system. out. println (strs1 [I]);} seventh, remove the same items in the array. We know that the items in the array can be repeated. Sometimes we need to remove the repeated items in the array. What should we do? We also know that there are no repeated items in the hashset. Can we convert the array into a hashset and then convert it back? String [] strs1 = new string [] {"1", "2", "6", "4", "2", "4 "}; hashset set = new hashset (); set. addall (arrays. aslist (strs1); string [] strs2 = (string []) set. toarray (New String [0]); For (INT I = 0; I <strs2.length; I ++) {system. out. println (strs1 [I]);} Eighth, we can copy some items of an array to another array, for example: string [] strs1 = new string [] {"1", "2", "3", "4", "5", "6 "}; string [] strs2 = new string [4]; system. arraycopy (strs1, 2, strs2, 0, 2); For (INT I = 0; I <strs2.length; I ++) {system. out. println (strs2 [I]);} the above section Code Is to copy the array strs1 to the array strs2, copy from the entry whose subscript is 2, copy to the entry whose subscript is 0 of strs2, and continue to copy two items. Ninth, if the length of an array is limited to a few, you can use the following method: String [] array; If (length. equals ("4") {array = new string [4];……} Else if (length. Equals ("9") {array = new string [9]; ......} Else if (length. Equals ("10") {array = new string [10]; ......} Else array = new string [] {}; if not, you can use reflection to generate an array, for example, try {int length = 10; Class CLS = Class. forname ("Java. lang. string "); object arr = array. newinstance (CLS, length); string [] STRs = (string []) Arr; For (INT I = 0; I <STRs. length; I ++) {STRs [I] = string. valueof (I) ;}for (INT I = 0; I <STRs. length; I ++) {system. out. println (STRs [I]);} catch (throwable e) {system. err. println (E );}
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.