Java Learning Note Seven--array tool class arrays

Source: Internet
Author: User

Array Tool class arrays

Some of the static modifiers contained in the arrays class provided by Java can manipulate arrays directly. If the method in the inside is cooked, then the development efficiency will be greatly improved. The methods are described below.

List<t> aslist (T ... a)

Function: Converts the specified array or array element to a fixed-size list.

Usage:

        String[] Strarr = {"AAA", "BBB", "VVV" };         // Usage 1: parameter is an array reference        list<string> list = arrays.aslist (strarr);          for (String str:list) {            System.out.println (str);        }         // Usage 2: parameter is an unlimited number of array elements        List<string> List2 = arrays.aslist ("AAA", "BBB", "VVV");          for (String str:list2) {            System.out.println (str);        }

Note: Although the parameter list is an indeterminate parameter, there are only two uses: either an array reference or multiple parameters with a consistent data type.

int BinarySearch (type[] a,type key)

Function: Use the binary method to query the index of the element key that appears in array A.

Type:char,byte,short,int,float,double,string and Custom objects.

Limitations: 1. If type is a basic data type or string, then the elements of array A must be arranged in ascending order;

2. If the type is a custom object, the object must implement the comparable interface (there is only one CompareTo method that specifies the object comparison size rule), and the elements of array a must be in ascending order.

Example:

        int [] intarr = {12,34,45,56,67,78,89};         int indexint = Arrays.binarysearch (Intarr,N);         // 4 = {"AAA", "BBB", "CCC", "VVV" };                          int indexstr = Arrays.binarysearch (Strarr, "CCC");         // 2

Here is an example of a type that is a custom object:

//custom objects, you must implement the comparable interface Public classModelImplementsComparable<model> {     Public intAge ; //A rule that specifies the size of a comparison between two objects, according to the order of the parameters,//If 1 is returned, the current object >m;//if 0 is returned, the current object is ==m;//If 1 is returned, the current object <m;//@Override Public intCompareTo (Model m) {if(Age >m.age) {            return1; }        if(age = =m.age) {            return0; }        //the rest is less than        return-1; }}
 Public classTest { Public Static voidMain (string[] args) {//defining 3 Model objectsModel M1 =NewModel (); M1.age= 20; Model m2=NewModel (); M2.age= 45; Model M3=NewModel (); M3.age= 32; //defines an array, in order from small to large, ascendingmodel[] MArr ={m1,m3,m2}; intINDEXM =Arrays.binarysearch (MARR, M3);  System.out.println (INDEXM); //1    }}
int BinarySearch (type[] a,int fromindex,int toindex,typekey)

Function: Similar to the previous method, only searches the elements in array a between the Fromindex index and the Toindex index.

Type:char,byte,short,int,float,double,string and Custom objects.

Limitation: Ibid.

Example: slightly

Type[] CopyOf (type[] original,int newlength)

Function: Copies the newlength elements of the array original, makes up a new array, and returns the new array.

Type:type: base type, String type, and custom object.

Note: If the newlength is less than the length of the original array, the first newlength elements of the original array are returned, and if the newlength is greater than the length of the original array, then an insufficient complement of 0 (numeric type), False (Boolean type), Null (reference type).

Example:

 Public classTest { Public Static voidMain (string[] args) {int[] Original = {1,2,3,4,5}; //less than original array length        int[] NewArr = arrays.copyof (Original, 3);  for(intI:newarr)  {System.out.println (i); // the        }                //greater than original array length        int[] newArr2 = arrays.copyof (Original, 8);  for(inti:newarr2)  {System.out.println (i); //1,2,3,4,5,0,0,0        }            }}
Type[] copyofrangetype[] original,int from,int to)

Function: Copies the elements (including from, not including to)in the index from the original to the new array and returns the new array.

Type: base type, String type, and custom object.

Example:

 import   java.util.Arrays;  public  class   Test { public  static  void   main (string[] args) { int  [] original = {1        , 2, 3, 4, 5};  int  [] newArr = Arrays.copyofrange (original, 0, 2         for  (int   I:newarr) {System.out.println (i);  //   }}}  
Boolean equals (type[] A1, type[] A2)

function, compares two arrays, returns true if the lengths of the A1 and A2 are equal, and each element of the A1 is equal to the element that the A2 corresponds to; returns false otherwise

Type: base type, String type, and custom type (implements comparable interface)

Example:

Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {string[] A1= {"AAA", "BBB", "CCC"}; string[] A2= {"AAA", "BBB", "CCC"}; String[] A3= {"AAA", "www", "CCC"}; BooleanA1A2 =Arrays.equals (a1, a2);  System.out.println (A1A2); //true                BooleanA1A3 =Arrays.equals (A1, A3);  System.out.println (A1A3); //false    }}
void Fill (type[] A,type val)

Function: Assigns all elements of array A to Val, overwriting if there is a value. Equivalent to populating array a with Val.

Type: base type, String type, and custom type

Example:

Import java.util.Arrays;  Public class Test {    publicstaticvoid  main (string[] args) {        new String[5];        " AAA ");          for (String str:a) {            System.out.println (str);   // Print 5 times "AAA"         }    }}
void Fill (type[] a,int fromindex,int toindex,type val)

Function: Fills the element in array a with Val from Fromindex to index Toindex, including Fromindex, not including Toindex

Type: base type, String type, and custom type

Example:

Import java.util.Arrays;  Public class Test {    publicstaticvoid  main (string[] args) {        = {"111", "222", "333", "444444", "5555555555" };         2, 4, "MMM");          for (String str:a) {            //  print "111", "222", "MMM", "MMM", "5555555555"        }    }}

Not to be continued ...

Java Learning Note Seven--array tool class arrays

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.