Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java uses the Arrays utility class

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how Java uses the Arrays tool class, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Arrays is a tool class provided by JDK for manipulating arrays. The Arrays class provides methods for dynamically creating, accessing, and manipulating Java arrays. This class also contains a static factory that allows you to view arrays as lists.

public static String toString(T [] a)

For example:

int[] a = {2,8,51,13,46,11,22};System.out.println(Arrays.toString(a));2, Array to set (1) asListpublic static List asList(T... a)

Example:

List list = Arrays.asList(1, 2, 3, 4, 5); System.out.println(list);

1) This method is applicable to arrays of object-type data (String, Integer…), and should not be used for arrays of primitive data types (byte,short,int,long,float,double,boolean);

2) This method links arrays with List lists: when one of them is updated, the other is automatically updated;

3) add(), remove(), clear() methods are not supported;

4) The length of the List obtained by this method is unchangeable;

5) If your List is only for traversal, use Arrays.asList(); if your List also adds or deletes elements, create a new java.util.ArrayList, and then add or delete elements one by one;

6) This ArrayList is not under java.util package, but java.util. Arrays.ArrayList. It is a static inner class defined by the Arrays class itself, which does not implement add(), remove() methods, but directly uses the corresponding methods of its parent class AbstractList.

public static IntStream (int[] array)

Example:

int[] a = {2,8,51,13,46,11,22};IntStream stream = Arrays.stream(a);System.out.println(Arrays.toString(stream.toArray()));System.out.println(Arrays.toString(a));

The array will be converted to streaming, streaming array processing, all available streaming processing methods.

(3) Collections.addAll()3. Sort arrays in ascending order public static void sort(T [] a)

Example:

int[] a = {20, 3, 32, 1, 72, 26, 35};Arrays.sort(a);4, determine whether the array is equal public static boolean equals(T[] a, T[] a2)

Example:

int[] a = {20, 3, 32, 1, 72, 26, 35};int[] b = {3, 5, 7, 8, 54, 23, 9};boolean boo = Arrays.equals(a, b);

The principle of comparison is equal length and equal elements.

//replace array with val public static void fill(T[] a, int fromIndex, int toIndex, T val)

Example:

int[] a = {1, 2, 3, 4};Arrays.fill(a, 0,2,5);6, copy array public static char[] copyOf(char[] original, int newLength)

Example:

int[] b = {3, 5, 7, 8, 54, 23, 9};int[] d = Arrays.copyOf(b, b.length);

Arrays 'copyOf() method returns the array as a new array object, and changing the values of the elements in the returned array does not affect the original array.

The second argument to copyOf() specifies the length of the new array to be created, and if the length of the new array exceeds the length of the original array, the array defaults are retained.

int binarySearch(byte[] a, byte key) //Query the location of the first occurrence of an element

Example:

int[] b = {3, 5, 7, 8, 9, 23, 54};int i = Arrays.binarySearch(b, 5);

Searches the specified array using a binary search method that returns the index value of the element to be searched for.

Note: The array must be sorted (sort method) before making this call. If the array is not sorted, the result is ambiguous. If an array contains more than one element with a specified value, there is no guarantee which one is found.

The search efficiency is faster than the average search time of searching from left to right in the array.

public static int hashCode(int a[]); //return hashCode value of array

Example:

int[] a = {1, 2, 3, 4};System.out.println(Arrays.hashCode(a));//Result: 955331 About "Java how to use Arrays tool class" This article is shared here, I hope the above content can have some help for everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report