The array tool class in Java

Source: Internet
Author: User

1, Boolean Equals (Array1,array2): Compares two arrays for equality.
Import Java.util.Arrays;
public class Ch03 {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String[] str1={"1", "2", "3"};
String[] str2={"1", "2", New String ("3")};
System.out.println (Arrays.equals (str1, str2));//The result is: true
}
}

2, void sort (array): The elements of an array of arrays are sorted in ascending order
//Order an array
int[] score ={79,65,93,64,88};
Arrays.sort (score);//Sort an array
//sort: The effect is to sort an array in small to large
//str is also a reference address
String str = arrays.tostring (score ); The
//print method can print the value of a string directly
System.out.println (str);
Arrays.tostring (Score): Transforms an array into a string
3, String toString (array): Converts an array of arrays into a string.
Import java.util.Arrays;
public class Ch05 {
public static void Main (string[] args) {
//TODO auto-generated method stub
//tostring: Put An array is converted to a string
//define an array
int[] a={1,2,3};
System.out.println (a);//Print out the Hashcode code
SYSTEM.OUT.PRINTLN (Arrays.tostring (a));//print out an array of
}
}
4, void Fill (array,val): assigns the array all elements to Val.

//fill method: Replace all elements in the array with a value of
int[] num={1,2,3};
//Parameter 1: Array object
//Parameter 2: Replacement value
Arrays.fill (num, 6);
SYSTEM.OUT.PRINTLN (arrays.tostring (num));//Print Result: [6, 6, 6]
5, int binarysearch (array,val): Query element value val in array subscript
//binarysearch: Find the subscript of the corresponding element by means of dichotomy
//Use premise: must be sorted before you can use
char[] a={' A ', ' B ', ' C ', ' d ', ' e '};
int i = Arrays.binarysearch (A, ' d ');
System.out.println (i);//The result is: 3

char[] b={' e ', ' A ', ' C ', ' B ', ' d '};
Arrays.sort (b);
Int J=arrays.binarysearch (b, ' e ');
System.out.println (j);
6, copyof (array,length): Copies an array of arrays into a new array of length. That is, expand the array size.
//copyof: Copy the contents of an existing array into a new array
int[] a={1,2,3};
//Parameter 1: original array parameter 2: Length of new array
int[] b=arrays.copyof (A, a.length+ 1);
System.out.println (arrays.tostring (b));
//a and B have different address codes

==============================================
Int[] Arr=new int[] {11,33,44,66,55,22,99,88,77};
Arrays.sort (arr);
System.out.println (arr);
System.out.println (arrays.tostring (arr));
System.out.println (Arrays.binarysearch (arr, 55));
Output
[[Email protected]
[11, 22, 33, 44, 55, 66, 77, 88, 99]
4

The array tool class in Java

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.