Introduction to the Java Util Arrays tool class

Source: Internet
Author: User
Tags java util

Import Java.util.arrays;import Java.util.comparator;public class Arraystest {/** * @Title: Main * @Description: * @param: * @return void * @user: WANGZG * @Date: 2014-9-11 * @throws */public static void Main (string[] args) {//TODO Auto-generat The Ed method Stub/*java.util.arrays class makes it easy to manipulate an array, and all of the methods it provides are static. Has the following functions: Assigning a value to an array: through the Fill method. Sort the array: By the Sort method, in ascending order. Compare arrays: Compares the values of elements in an array by the Equals method. Find array elements: The BinarySearch method can be used to perform binary lookups of sorted arrays. */int arrint[] = new int[10];//fills the array System.out.println ("padding array, 3"); Arrays.fill (Arrint, 3), Output (arrint),//assigns the 2nd and 3rd elements of the array to 8SYSTEM.OUT.PRINTLN ("assigns the 2nd and 3rd elements of the array to 8"); Arrays.fill (Arrint, 2, 4, 8), output (arrint);//Sort by 2nd to 6th of the array System.out.println ("Sort the 2nd to 6th of an array"); Arrays.sort (arrint,2,7); output (arrint);//Sort the entire array System.out.println ("Sort the entire array"); Arrays.sort (arrint); output (arrint); int array1[] = new Int[]{1,3,3,4,6,5,7,8,9,0,11,12};int array2[] = new int[]{1,2,3 };//compares array elements for equality (two arrays are equal if two arrays contain the same elements in the same order). In addition, if two array references are NULL, they are considered equal. ) System.out.println ("Compares array elements for equality: Arrays.equals (ARray1, Array2): "+" \ n "+arrays.equals (Array1, array2)); Array2 = Array1.clone (); System.out.println ("The array elements are equal after cloning: Arrays.equals (Array1, Array2):" + "\ n" +arrays.equals (Array1, array2));// Use a binary search algorithm to find the subscript where the specified element is located (it must be sorted, otherwise the result is incorrect, and if the array contains more than one element equal to the specified object, it is not guaranteed which one is found.) ) Arrays.sort (array1); output (array1); System.out.println ("element 3 in array1 position: Arrays.binarysearch (Array1, 3):" + "\ n" +arrays.binarysearch (Array1, 3));// Returns a negative number if it does not exist System.out.println ("element 9 in Arrint position: Arrays.binarysearch (Arrint, 9):" + "\ n" +arrays.binarysearch (Arrint, 9 );//Convert to String System.out.println ("Convert to string"); String str = arrays.tostring (array1); System.out.println (str);D og arrdog[] = new Dog[4];arrdog[0] = new Dog (3); arrdog[1] = new Dog (1); arrdog[2] = new Dog (0); arr DOG[3] = new Dog (2), for (int i=0; i<arrdog.length;i++) {System.out.print (arrdog[i]+ "\ t");} System.out.println ();//Sort the array by a custom comparer System.out.println ("Sort arrays by custom comparer"); Arrays.sort (arrdog,new comparator<dog> () {public int compare (Dog dog1,dog dog2) {int age1 = Dog1.getage (); int Age2 = DoG2.getage ();  return age2-age1; }}); for (int i=0; i<arrdog.length;i++) {System.out.print (arrdog[i]+ "\ t");}} /** * * @Title: Output * @Description: * @param: * @return void * @user: WANGZG * @Date: 2014-9-11 * @throws */public STA tic void output (int[] array) {if (array!=null) {for (int i = 0; i < Array.Length; i++) {System.out.print (array[i]+ ""); }}system.out.println ();}} class dog{private int age;public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Dog () {}public Dog (int.) {this.age = age;} @Overridepublic string toString () {String str = "[Age:" +this.age+ "]"; return str;}}

Introduction to the Java Util Arrays tool class

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.