Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Arrays Array tool Class in Java

Shulou Source: shulou.com Published: 2022-06-02 07:03:07 11月08日 Update

Editor to share with you how to use the Arrays array tools in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Method list method name briefly describes asList () returns a list of fixed sizes supported by the specified array. Sort () sort the array (ascending order) parallelSort () sort the specified array in ascending order binarySearch () use the binary search method to quickly find the specified value (provided the array must be ordered) compare () compare two arrays compareUnsigned () compare two arrays in dictionary order Process numeric elements as unsigned copyOf () fill copy array copyOfRange () copy the specified range of the array to a new array equals () compare two arrays deepEquals () compare two array depths toString () convert an array to a string deepToString () convert a multidimensional array to a string fill () populate each element of the array mismatch () find and return the first mismatched index between the two arrays If not found, return-1parallelPrefix () use the provided function to operate on the array elements parallelSetAll () use the provided generator function to set up all elements of the specified array in parallel to calculate each element setAll () use the provided generator function to set all the elements of the specified array to calculate the fast positioning detailed operation of each element

Next, I'll use the code to illustrate one by one.

AsList ()

Function: returns a list of fixed sizes supported by the specified array

Parameter: asList (T … a)

Return value: a list

Code example:

@ Test public void asListTest () {List ss = Arrays.asList ("hello", "world"); / / List ss1 = Arrays.asList ("hello", "world", 1); the type must always (generic) System.out.println (ss); / / [hello, world] / / ss.add ("java"); / / UnsupportedOperationException will report errors / / ss.remove (1) / / UnsupportedOperationException will report System.out.println (ss.get (0)); / / hello ss.set (0, "java"); System.out.println (ss); / / [java, world]}

Be careful

After converting this array to a list, the corresponding list does not support add and delete operations, otherwise an error will be reported.

But you can modify and get elements

ToString () and deepToString ()

Function: convert an array to a string

Parameters: array to be converted

Return value: converted string

Code example:

@ Test public void asListTest () {List ss = Arrays.asList ("hello", "world"); / / List ss1 = Arrays.asList ("hello", "world", 1); the type must always (generic) System.out.println (ss); / / [hello, world] / / ss.add ("java"); / / UnsupportedOperationException will report errors / / ss.remove (1) / / UnsupportedOperationException will report System.out.println (ss.get (0)); / / hello ss.set (0, "java"); System.out.println (ss); / / [java, world]} sort () and parallelSort ()

Function: all sort the array (default ascending order, support lambda, generic)

Parameters:

Sort (Object [] a [, int fromIndex, int toIndex])

Or sort (T [] a [, int fromIndex, int toIndex,] Comparator

Tags: Array parameter element range code function example two cantilever support order function dictionary method type report character string length sort Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information MySQL MariaDB OPPO Reno