Java Programming Diary 1

Source: Internet
Author: User
Tags sorts

First,java.util.Arrays

1.equals

Compares two non-identical arrays for equality, while the equals of the array itself determines whether another array is itself.

Declaration: PublicStatic Boolean equals (TYPE[]A,TYPE[]A2)

Parameters:A- an array that will test its equality. A2- Another array that will test its equality. The type of the parameter can be either a primitive data type or any type of reference type.

Returns: Returns Trueif two are equal.

Description: If two arrays contain the same number of elements, and all corresponding elements in the two array correspond to equal, the two arrays are considered equal.

In addition, if two array references are null, they are considered equal. e1==null e2=null:e1.equals (E2), E1 and E2 Two objects are considered equal.

Note:equals () compares the values stored in the heap memory, "= =" is not sure, please refer to my other blog post "comparison Trap".

2,deepequals

Declaration: PublicStatic Boolean deepequals (object[] A1, object[] A2)

Description: If two array references are null, or if they refer to an array containing the same number of elements, and all corresponding element pairs in the two array are deeply equal, the two array references are considered to be deep and equal. In other words, the first-level reference elements correspond to equality, that is, deep equality.

In the following cases, two null elements E1 and E2 may be deeply equal.

A) E1 and E2 are both arrays of object reference types, and arrays.deepequals (E1, E2) returns true.

b) E1 and E2 are both arrays of the same base type, and the appropriate overloads of Arrays.equals (E1, E2) will return true .

c) E1 = = E2

d) e1.equals (E2) will return true

Parameters:

A1- An array that will test its equality

A2- Another array that will test its equality

Returns: Returns trueif two specified arrays are deeply equal to each other,otherwise false.

Precautions:

A) This method supports null elements of any depth .

b) If any of the arrays in the specified array are either directly or indirectly through one or more array levels, containing the arrays themselves as their elements, the result of this method is ambiguous, even if the reference element in the array with this method cannot contain it itself.

3.Fill- assigns a value to the specified array element

all elements that act on an array

public static void Fill (type[] A, type Val)

elements that act on the specified range of an array

public static void Fill (type[] A, int fromIndex, int toindex, type val)

Description: Assigns the specified type value to the element. If you specify an element range for an array of types (fromidex to toindex ( not included )), the value is assigned to each element in the specified range, Otherwise, all elements of the array are assigned. If fromindex==toindex, the padding range is empty.

Parameters:

A- the array to populate.

Val-the value to store in the array element

Type- the data type, which can be any type (including native data types and object data types).

FromIndex-the index (including) of the first element to be populated with the specified value.

Toindex-The index of the last element to be populated with the specified value (not included).

4, sort -Sort

all elements that act on an array

public static void sort (type[] a)

elements that act on the specified range of an array

public static void sort (type[] A, int fromIndex, int toindex)

Describe:

Sorts all elements (or elements within a specified range) of the specified type (any native data type except Boolean) in ascending order of number.

an array of type object that sorts the specified array of objects in ascending order, based on the natural ordering of the elements.

(fromindex==toindex, the sort range is empty)

Parameters:

A- the array to sort.

FromIndex- Index of the first element to sort (includes)

Toindex- The last element to sort (not included)

Type-the data type can be all native data and object type data except the Boolean type .

Note:

1) An array of double,float types, using Double.comparto (java.lang.Double) to force the use of full-order ordering, which is considered -0.0<0.0 and nan> Any other floating-point values, and all NaN values are equivalent and equal. the < relationship is not adopted because it is not possible to achieve full order ordering.

2) for object sort, note the following:

A) all elements in an array of type object must implement the comparable interface, which can be compared to each other. In other words, E1.compareto (E2) must not throw any of the E1 and E2 elementsin the array. classcastexception .

b) for An array of type object, this sort does not reorder the elements that are equal to ensure the stability of the array.

c) for An array of type object, the sorting algorithm uses an improved merge sorting algorithm (if the highest element in the lower sub-list is less than the lowest element in the Taka list, the merge is ignored).

sort by using the specified comparer

Method declaration:

public static <T> void sort (t[] A, comparator<? Super T> C)

Or

public static <T> void sort (t[] A, int fromIndex, int toindex, comparator<? Super T> C)

Description: Sorts the specified array of objects (which can be specified) according to the order produced by the specified comparer. All elements in an array (or in the specified range of an array) must be compared by specifying a comparer. That is,c.compare (e1,e2) must not throw any E1 and E2 elements that are in the array (which can specify the range) ClassCastException.

Parameters:

A- the array to sort.

C- The comparer that determines the order of the array. a null value indicates the natural order in which the element is used.

FromIndex-the index (including) of the first element to sort.

Toindex-The index of the last element to sort (not included).

Precautions:

1) This sort does not reorder the elements that are equal to ensure the stability of the array.

2) The sorting algorithm uses an improved merge sorting algorithm (if the highest element in the lower sub-list is less than the lowest element in the Taka list, the merge is ignored).

5,binarysearch- use binary search algorithm to search the specified array

Statement:

public static int BinarySearch (type[] A, type key)

public static int BinarySearch (long[] a,int fromindex,int toindex,long key)

public static <T> int BinarySearch (t[] A, T key, comparator<? Super T> C)

public static <T> int BinarySearch (t[] a,int fromindex,int toindex,t key,comparator<? Super T> C)

Describe:

Searches the specified type array using a binary search algorithm to obtain the specified value.

Parameters:

A- the array to search for.

Key- The value to search for.

FromIndex-the index (including) of the first element to sort.

Toindex-The index of the last element to sort (not included).

Type-byte,double,float,object,long ,int,short,char,

C- The comparer that is used to sort the array. a null value indicates that the natural order of the elements should be used.

Return:

Returns the index of Key If the search succeeds, otherwise returns (insertion point -1). The insertion point is defined as the point at which the key is inserted into the array, that is, if all elements in the array are smaller than the specified key, the return is greater than the key or the array length ( The first element of the a.length). Note: This guarantees that the return value will be >=0if and only if key is found.

Precautions:

1.You must sort the array before this method call ( using the sort () method ). Otherwise, the result is indeterminate.

2. If the array contains more than one element with the specified value, it is not guaranteed which one to hit.

3. Whentype is double,float , this method considers NaN The values are both equivalent and equal.

Java Programming Diary 1

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.