Arrays Class
An array of tool classes that provide a number of static methods for manipulating arrays;
Not much to say, directly on the example
Example://Declaration Package package array;//Import Package//role: Tell the system, Arraysdemo class used to arrays class to java.util package to find arrays class import java.util.arrays;/** * common ways to demonstrate Arrays class * @author Genius Alliance - Yukun */public class arraysdemo {public static void main (String[] args) {// Declares three arrays of int[] array1 = { 1, 2, 3 };int[] array2 = { 1, 2, 3 };int[] array3 = { 1, 2, 3, 6, 5, 4 }; System.out.println ("---------- compare two arrays ----------");//Compare the contents of the array array1 and Array2 boolean Compare12result = arrays.equals (array1, array2);//Compare the contents of the array array2 and Array3 boolean Compare23result = arrays.equals (ARRAY1, ARRAY3); System.out.println ("Array1 and array2 the results of the comparison:" +compare12result); System.out.println ("Array2 and Array3 compare to:" +compare23result);/* * first parameter: Incoming copied array, second parameter: new array length copied * When the length of the new array is greater than the old array length, the extra part is filled with the default value corresponding to the data type * ARRAY1:[1, 2, 3] Use the following code to copy after arraycopy:[1, 2, 3, 0, 0] */int[] arraycopy = arrays.copyof (array1, 5); System.out.println ("----------- original array -----------");//traverse the original array arraytostring (ARRAY1); System.out.println ("----------- copied array -----------");//Iterate over the copied array arraytostring (arrayCopy); SYSTEM.OUT.PRINTLN ("array ----------before---------- replace value");//iterate over the array array1arraytostring (ARRAY1) before replacing; System.out.println (array ----------after---------- replace value);/* * replaces all values in the array with the same value * First parameter: Replaced array * second parameter: Replaced value */arrays.fill (ARRAY1, 10);//traversal of replaced array array1arraytostring (ARRAY1) ; System.out.println ("---------- array before sorting ----------");//The array array3arraytostring (ARRAY3) before traversing the sort;// Sort the array array3 (only in ascending order) Arrays.sort (ARRAY3); System.out.println ("---------- sorted array ----------");//Iterate through the sorted array array3arraytostring (ARRAY3); SYSTEM.OUT.PRINTLN ("----- using dichotomy (binary) to find elements -----");//using dichotomy to find elements in an array, the lookup array must be sorted in ascending order int Index = arrays.binarysearch (array3, 4); System.out.println ("4 in the array array3 subscript is:" + index);} /** * converts an array to a string and outputs */private static void arraytostring (Int[] array) {//converts the contents of an array to a string string arraystr = arrays.tostring (array); System.out.println (ARRAYSTR);}} Run Result:--------- compare two arrays ---------array1 and Array2 compare to: false----------- original array, truearray2 and Array3 -----------[1, 2, 3]----------- copied arrays -----------[1, 2, 3, 0, 0] Array before--------- replace value ---------[1, 2, 3]--------- Replace value after the array ---------[10, 10,  10]--------- array before sorting ---------[1, 2, 3, 6, 5, 4]--------- sorted array ----------[1, 2, 3, 4, 5, 6]----- Find elements -----using dichotomy (binary) 4 the subscript in the array array3 is: 3
This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!
I genius the official free tutorial 27: Java Basic teaching Arrays class