Application of arrays class in Java

Source: Internet
Author: User

The Java.util.Arrays class makes it easy to manipulate an array, and all of the methods it provides are static. Has the following features:

² assign a value to an array: through the fill method.

² Array Sorting: By the sort method , in ascending order.

² Compare Arrays: compares the values of elements in an array by the Equals method.

² Find Array elements: the binarysearch method can be used to perform binary lookups of sorted arrays.

Import Java.util.Arrays;

public class Testarrays {

public static void output (Int[] array) {

if (array!=null) {

for (int i = 0; i < Array.Length; i++) {

System.out.print (array[i]+ "");

}

}

System.out.println ();

}

public static void Main (string[] args) {

int[] array = new INT[5];

Populating an array

Arrays.fill (array, 5);

System.out.println ("padding array: Arrays.fill (Array, 5):");

Testarrays.output (array);

Assigns the 2nd and 3rd elements of an array to a value of 8

Arrays.fill (Array, 2, 4, 8);

SYSTEM.OUT.PRINTLN ("assigns the 2nd and 3rd elements of the array to 8:arrays.fill (array, 2, 4, 8):");

Testarrays.output (array);

Int[] array1 = {7,8,3,2,12,6,3,5,4};

Sort from 2nd to 6th of an array

Arrays.sort (array1,2,7);

System.out.println ("Sort the 2nd to 6th elements of an array: Arrays.sort (array,2,7):");

Testarrays.output (array1);

To sort the entire array

Arrays.sort (array1);

System.out.println ("Sort the entire array: Arrays.sort (array1):");

Testarrays.output (array1);

Compare array elements for equality

System.out.println ("Compare array elements for equality: arrays.equals (Array, array1):" + "\ n" +arrays.equals (array, array1));

int[] Array2 = Array1.clone ();

System.out.println ("The array elements are equal after cloning: Arrays.equals (Array1, Array2):" + "\ n" +arrays.equals (Array1, array2));

Use a binary search algorithm to find the subscript where the specified element is located (must be ordered, otherwise the result is incorrect)

Arrays.sort (array1);

System.out.println ("element 3 in array1 position: Arrays.binarysearch (Array1, 3):" + "\ n" +arrays.binarysearch (Array1, 3));

Returns a negative number if it does not exist

System.out.println ("element 9 in array1 position: Arrays.binarysearch (array1, 9):" + "\ n" +arrays.binarysearch (Array1, 9));

}

}

Output Result:

Padding array: Arrays.fill (Array, 5):
5 5 5) 5 5
Assigns the 2nd and 3rd elements of an array to 8:arrays.fill (array, 2, 4, 8):
5 5 8) 8 5
Sort the 2nd to 6th elements of an array: Arrays.sort (array,2,7):
7 8 2 3 3 6 12 5 4
To sort the entire array: Arrays.sort (array1):
2 3 3 4 5 6 7 8 12
Compare array elements for equality: arrays.equals (Array, array1):
False
Whether the array elements are equal after cloning: Arrays.equals (Array1, array2):
True
Element 3 position in Array1: Arrays.binarysearch (Array1, 3):
1
Element 9 position in Array1: Arrays.binarysearch (array1, 9):
-9

Application of arrays 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.