Java Array (4): Array utility function

Source: Internet
Author: User
Tags comparable shallow copy

The System.arraycopy () method of the Java Standard Class library, and a set of static methods for arrays in the Java.utils.Arrays class, are practical functions of manipulating arrays. The following are described separately.

(1) Copying of arrays

(2) Comparison of arrays

(3) Sorting and finding of arrays

(1) Copying of arrays

System.arraycopy (source array, the offset from where the source array starts copying, the target array, the offset from where the array of marks begins copying, the number of elements that need to be copied)

System.arraycopy has been overloaded with all the basic types (the same wrapper type), and if the object is copied, it is simply a shallow copy (copy reference).

The following is an example of an int:

1 Importjava.util.Arrays;2 3  Public classTest1 {4      Public Static voidMain (string[] args) {5         int[] i =New int[3];6         int[] J =New int[7];7Arrays.fill (I, 47);8Arrays.fill (J, 99);9System.out.println ("i =" + arrays.tostring (i));//i = [J, A, a]TenSystem.out.println ("j =" + Arrays.tostring (j));//j = [ to the most, the most, the most, the most, the most, One         int[] k =New int[5]; AArrays.fill (K, 103); -System.arraycopy (i, 0, K, 0, i.length); -System.out.println ("k =" + arrays.tostring (k));//k = [103, 103] theSystem.arraycopy (k, 1, J, 0, 4);// -System.out.println ("j =" + Arrays.tostring (j));//j = [103, 103 , A.,] -     } -}

(2) Comparison of arrays

Similarly, Arrays.equals has been overloaded with all the basic types (the same wrapper type), and for object comparisons, the Equals method of the object is used, so the Equals method of the object must be overridden .

1 Importjava.util.Arrays;2 3  Public classTest2 {4      Public Static voidMain (string[] args) {5         int[] A1 =New int[5];6         int[] A2 =New int[5];7Arrays.fill (A1, 47);8Arrays.fill (A2, 47);9System.out.println (Arrays.equals (a1, a2));//trueTenA2[3] = 11; OneSystem.out.println (Arrays.equals (a1, a2));//false Astring[] S1 =NewString[4]; -Arrays.fill (S1, "Hi"); -string[] s2 = {"Hi", "Hi",NewString ("Hi"),NewString ("Hi") }; theSystem.out.println (Arrays.equals (S1, S2));//true -     } -}

(3) Sorting and finding of arrays

Similarly, Arrays.sort has been overloaded with all the basic types (the same as the wrapper type), and the comparable interface must be implemented for object comparisons.

If the array is already sequenced, you can use Arrays.binarysearch to perform a quick lookup ( provided that it must be an orderly array).

If you use comparator<t> to sort an array of objects, you must provide the same comparator<t> when you use Arrays.binarysearch.

1 Importjava.util.Arrays;2 Importjava.util.Collections;3 ImportJava.util.Random;4 5 classComptypeImplementsComparable<comptype> {6     inti;7     intJ;8 9      PublicComptype (intN1,intn2) {Teni =N1; Onej =N2; A     } -  - @Override the      PublicString toString () { -         return"(" + i + "," + j + ")"; -     } -  + @Override -      Public intcompareTo (Comptype ct) { +         returni = = ct.i?Integer.compare (J, CT.J): Integer.compare (i, ct.i); A     } at } -  -  Public classTest3 { -      Public Static voidMain (string[] args) { -  -         //Basic Type inRandom random =NewRandom (47); -         int[] A = Random.ints (5, 5, 10). ToArray (); toSystem.out.println (Arrays.tostring (a));//[8, 5, 8, 6, 6] +Arrays.sort (a);//Arrays.sort (basic type) -System.out.println (Arrays.tostring (a));//[5, 6, 6, 8, 8] the  *         //Package Type $Integer[] B = {3, 5, 9, 8, 2 };Panax NotoginsengArrays.sort (b);//Arrays.sort (Object) -System.out.println (arrays.tostring (b));//[2, 3, 5, 8, 9] theArrays.sort (b, Collections.reverseorder ());//Arrays.sort (Object, comparator<t>) +System.out.println (arrays.tostring (b));//[9, 8, 5, 3, 2] A  the         //String +String[] C = {"A", "B", "AB", "AC", "a", "B", "AB", "AC" }; -Arrays.sort (c);//string default is dictionary sort [a-za-z] $System.out.println (Arrays.tostring (c));//[A, AB, AC, B, A, AB, AC, b] $Arrays.sort (c, String.case_insensitive_order);//Ignore case Sort -System.out.println (Arrays.tostring (c));//[A, A, AB, AB, AC, AC, B, b] -  the         //Object Type -Comptype[] D = {NewComptype (2, 2),NewComptype (1, 2),NewComptype (2, 4),NewComptype (0, 3),Wuyi                 NewComptype (3, 4),NewComptype (3, 0),NewComptype (2, 2),NewComptype (2, 1) }; the Arrays.sort (d); -System.out.println (arrays.tostring (d));//[(0, 3), (1, 2) , (2, 1), (2, 2), (2, 2), (2, 4), (3, 0), (3, 4)] Wu Arrays.sort (d, Collections.reverseorder ()); -System.out.println (arrays.tostring (d));//[(3, 4), (3, 0) , (2, 4), (2, 2), (2, 2), (2, 1), (1, 2), (0, 3)] About  $         //Quick Find - Arrays.sort (b); -         intLocation = Arrays.binarysearch (b, 8); -System.out.println ("Location of [5] was" + location + ", b[" + location + "] =" + b[location]);//Location of [5] is 3, b[3] = 8 A  + Arrays.sort (c); theLocation = Arrays.binarysearch (c, "AC"); -System.out.println ("Location of [AC] was" + location + ", c[" + location + "] =" + c[location]);//Location of [AC] is 2, c[2] = AC $  the Arrays.sort (c, string.case_insensitive_order); theLocation = Arrays.binarysearch (c, "AC", String.case_insensitive_order); theSystem.out.println ("Location of [AC] was" + location + ", c[" + location + "] =" + c[location]);//Location of [AC] is 5, c[5] = AC the  - Arrays.sort (d); inLocation = Arrays.binarysearch (d,NewComptype (2, 4)); theSystem.out.println ("Location of (2, 4) are" + location + ", d[" + location + "] =" + d[location]);//Location of (2, 4) is 5, d[5] = (2, 4) the     } About}

Java Array (4): Array utility function

Related Article

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.