Summary of common methods of java.util.Arrays, java.lang.Math, java.lang.System classes

Posted by dodginess on Wed, 30 Oct 2019 22:44:47 +0100

The java.util.Arrays class is a tool class of arrays. The common methods of arrays include

Binary search: public static int binarySearch(array[],int key), which returns the index of the key's subscript

Expand and shrink: public static int [], copyOf(array [], newLength), return new array

Take part: public static int [], copyofrange (array [], Frontex, toindex);, note that [from,to) is an open interval, and returns a new array.

Quick sorting of arrays from small to large: public static void array.sort (array [])

Overall assignment: public static void fill(array[],value),

Output as string: public static String (toString(array []), return string

Check for equality: public static boolean equals(array1[],array2 []), return Boolean value

 

java.lang.Math class is a commonly used math class. The commonly used methods are:

Take the absolute value: public static int abs(int value), return the absolute value

Round up: public static int ceil(double value),

Round down: public static int floor(double value);

public static long round(double value);

[0,1)de random number: public static double random();

 

The java.lang.System class has many common system functions:

Take the number of milliseconds between the current system time and 0:00 on January 1, 1970: public static long currentTimeMillis()

Array copy and paste: public static void arrayCopy(Object src, int srcPos, Object dest, int destPos, int length):

* src: source array

* srcPos: start subscript of source array

* dest: target array

* destPos: start index of the target array

* src[srcPos] element in dest[destPos]

* storage in sequence

* length: several copies in total

  *
* if src and dest are the same array, when [srcpos] > [destpos], move from the back to the front, generally used for element deletion.
* if src and dest are the same array, when [srcpos] < destpos], move forward and backward.

 

Provide an example to deepen your impression of consolidation:

 1 import java.lang.reflect.Array;
 2 import java.util.Arrays;
 3 import java.util.Scanner;
 4 
 5 public class ArraysUtil {
 6 
 7     public static void main(String[] args) {
 8         double[] array=new double[100];
 9         // TODO Auto-generated method stub
10         for(int i=0;i<=Array.getLength(array)-1;i++) {
11             double num=Math.random()*1000-500;
12             array[i]=num;
13         }
14         System.out.print(Arrays.toString(array));//Convert array to string and output
15         double[] array1=new double[array.length];
16         array1=Arrays.copyOf(array, array.length);//Copy the entire array length to array1
17         Arrays.sort(array);//Fast array
18         System.out.println();
19         System.out.print(Arrays.toString(array));//Output fast array
20         System.out.println();
21         System.out.println(Arrays.equals(array, array1));//Determine whether the array after quick sorting is equal to the array before quick sorting
22         System.out.println(Arrays.toString(Arrays.copyOfRange(array, 0, 10)));//Copy 10 elements from subscript 0 to return array and convert to string output
23         System.out.println(Arrays.toString(Arrays.copyOfRange(array, array.length-10, array.length)));
24         double num=Math.random()*1000-500;
25         Arrays.fill(array1, num);//Assign all elements of the array to num
26         System.out.println(Arrays.toString(array1));
27         
28         for(int i=0;i<=array.length-1;i++) {
29             array[i]=Math.abs(array[i]);//Absolute value of array element
30         }
31         System.out.println(Arrays.toString(array));
32         System.out.println(System.currentTimeMillis());//Output current system time, distance 1970.1.1 0:0:0 Millisecond count
33         
34         for(int i=0;i<=array.length-1;i++) {
35             System.out.print(Math.ceil(array[i])+" ");//Rounding up
36         }
37         System.out.println();
38         System.out.println(System.currentTimeMillis());
39         
40         for(int i=0;i<=array.length-1;i++) {
41             System.out.print(Math.floor(array[i])+" ");//Rounding down
42         }
43         System.out.println();
44         System.out.println(System.currentTimeMillis());
45         
46         for(int i=0;i<=array.length-1;i++) {
47             array[i]=Math.round(array[i]);//Rounding
48         }
49         Arrays.sort(array);
50         System.out.println(Arrays.toString(array));
51         System.out.println(System.currentTimeMillis());
52         
53         System.out.println("input");
54         Scanner input= new Scanner(System.in);
55         int key=input.nextInt();
56         int index=Arrays.binarySearch(array, key);//The binary search value is key Subscript
57         System.out.println(index);
58         System.arraycopy(array, index+1, array, index, array.length-index-1);//Cover up key
59         array=Arrays.copyOf(array, array.length-1);//Array shrinkage
60         System.out.println(Arrays.toString(array));    
61 
62         //stay key1 Add a random element at
63         System.out.println("add");
64         int key1=input.nextInt();
65         int num1=(int)(Math.random()*1000);
66         double[] array2=Arrays.copyOfRange(array, key1, array.length);//Array array Subscript from subscript key1 Copy to the last subscript. Note that it is an open interval, so add one.
67         array=Arrays.copyOf(array, array.length+1);
68         System.arraycopy(array2, 0, array, key1+1, array2.length);
69         array[key1]=num1;
70         System.out.println(Arrays.toString(array));
71     }
72 }

Results are not unique:

[370.5069491677524, 196.6812635069174, -360.5048683247735, -296.9707345890539, -248.18275801431668, 330.4816228586669, -83.76315619698659, 123.90888214798963, -288.4938711174634, 347.11337149526446, -237.63606767620138, 234.0763833448758, -423.2174433068593, 437.5319335124543, 477.39494908857966, -63.52554800479061, -189.15434718636624, -420.3981620197292, -356.635413884099, -118.68024437258975, -249.08160966211946, -164.64062114908205, 267.73263503815247, -244.01698981975795, 75.80884356006504, -161.0076696340871, 186.41421827809688, -201.74529345276937, -403.34287177804606, -424.7018129947752, -407.4372958785374, 439.8874670618925, 235.03475272891728, 451.7032900989036, -94.63135625309371, 93.7514920375761, -270.67989653240284, 226.49579129131337, 448.0221366776857, 360.63408120779377, -306.7179449434235, -428.25511855314755, -434.8824518357589, 350.6605431511523, 140.06152121597563, -78.5502205758545, -122.83652551630274, 389.50821223909304, 480.24820355724114, -216.21815809402455, 343.492615987632, 111.03136849447219, 304.50815037151335, -119.14606088041768, -315.2499951269141, -174.39057295279406, -30.77496151941648, 320.27070664221264, -497.7300739116909, 97.47672144505884, 117.87618615257918, 499.78473861773705, 124.03566531027604, -335.5127458556708, -208.62790871079818, -400.2969248122029, -194.45216537264133, 190.2127774328835, -292.6538311060517, 327.2592714625607, 418.8112178981787, -149.03121967343571, -378.6113204198135, 42.986234712738224, 173.78158362663407, 429.3069792635346, 422.4776976379379, -478.8039735767445, 233.99766587778856, 410.70952597237647, -239.6336454635926, 374.991995979983, 311.7558922775456, -438.3354136628692, -445.55510898513694, -389.19001872114865, 458.0682753336263, -395.001482516242, -308.39735963034764, -131.69462332131934, 298.7768215584704, -97.71464522752058, -415.12625464615985, -351.5274307617464, -89.41943095578017, -496.6507460368589, 35.157530483676965, 236.20433842160116, 379.3641021486826, -470.67865296241206]
[-497.7300739116909, -496.6507460368589, -478.8039735767445, -470.67865296241206, -445.55510898513694, -438.3354136628692, -434.8824518357589, -428.25511855314755, -424.7018129947752, -423.2174433068593, -420.3981620197292, -415.12625464615985, -407.4372958785374, -403.34287177804606, -400.2969248122029, -395.001482516242, -389.19001872114865, -378.6113204198135, -360.5048683247735, -356.635413884099, -351.5274307617464, -335.5127458556708, -315.2499951269141, -308.39735963034764, -306.7179449434235, -296.9707345890539, -292.6538311060517, -288.4938711174634, -270.67989653240284, -249.08160966211946, -248.18275801431668, -244.01698981975795, -239.6336454635926, -237.63606767620138, -216.21815809402455, -208.62790871079818, -201.74529345276937, -194.45216537264133, -189.15434718636624, -174.39057295279406, -164.64062114908205, -161.0076696340871, -149.03121967343571, -131.69462332131934, -122.83652551630274, -119.14606088041768, -118.68024437258975, -97.71464522752058, -94.63135625309371, -89.41943095578017, -83.76315619698659, -78.5502205758545, -63.52554800479061, -30.77496151941648, 35.157530483676965, 42.986234712738224, 75.80884356006504, 93.7514920375761, 97.47672144505884, 111.03136849447219, 117.87618615257918, 123.90888214798963, 124.03566531027604, 140.06152121597563, 173.78158362663407, 186.41421827809688, 190.2127774328835, 196.6812635069174, 226.49579129131337, 233.99766587778856, 234.0763833448758, 235.03475272891728, 236.20433842160116, 267.73263503815247, 298.7768215584704, 304.50815037151335, 311.7558922775456, 320.27070664221264, 327.2592714625607, 330.4816228586669, 343.492615987632, 347.11337149526446, 350.6605431511523, 360.63408120779377, 370.5069491677524, 374.991995979983, 379.3641021486826, 389.50821223909304, 410.70952597237647, 418.8112178981787, 422.4776976379379, 429.3069792635346, 437.5319335124543, 439.8874670618925, 448.0221366776857, 451.7032900989036, 458.0682753336263, 477.39494908857966, 480.24820355724114, 499.78473861773705]
false
[-497.7300739116909, -496.6507460368589, -478.8039735767445, -470.67865296241206, -445.55510898513694, -438.3354136628692, -434.8824518357589, -428.25511855314755, -424.7018129947752, -423.2174433068593]
[422.4776976379379, 429.3069792635346, 437.5319335124543, 439.8874670618925, 448.0221366776857, 451.7032900989036, 458.0682753336263, 477.39494908857966, 480.24820355724114, 499.78473861773705]
[15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188, 15.062960922778188]
[497.7300739116909, 496.6507460368589, 478.8039735767445, 470.67865296241206, 445.55510898513694, 438.3354136628692, 434.8824518357589, 428.25511855314755, 424.7018129947752, 423.2174433068593, 420.3981620197292, 415.12625464615985, 407.4372958785374, 403.34287177804606, 400.2969248122029, 395.001482516242, 389.19001872114865, 378.6113204198135, 360.5048683247735, 356.635413884099, 351.5274307617464, 335.5127458556708, 315.2499951269141, 308.39735963034764, 306.7179449434235, 296.9707345890539, 292.6538311060517, 288.4938711174634, 270.67989653240284, 249.08160966211946, 248.18275801431668, 244.01698981975795, 239.6336454635926, 237.63606767620138, 216.21815809402455, 208.62790871079818, 201.74529345276937, 194.45216537264133, 189.15434718636624, 174.39057295279406, 164.64062114908205, 161.0076696340871, 149.03121967343571, 131.69462332131934, 122.83652551630274, 119.14606088041768, 118.68024437258975, 97.71464522752058, 94.63135625309371, 89.41943095578017, 83.76315619698659, 78.5502205758545, 63.52554800479061, 30.77496151941648, 35.157530483676965, 42.986234712738224, 75.80884356006504, 93.7514920375761, 97.47672144505884, 111.03136849447219, 117.87618615257918, 123.90888214798963, 124.03566531027604, 140.06152121597563, 173.78158362663407, 186.41421827809688, 190.2127774328835, 196.6812635069174, 226.49579129131337, 233.99766587778856, 234.0763833448758, 235.03475272891728, 236.20433842160116, 267.73263503815247, 298.7768215584704, 304.50815037151335, 311.7558922775456, 320.27070664221264, 327.2592714625607, 330.4816228586669, 343.492615987632, 347.11337149526446, 350.6605431511523, 360.63408120779377, 370.5069491677524, 374.991995979983, 379.3641021486826, 389.50821223909304, 410.70952597237647, 418.8112178981787, 422.4776976379379, 429.3069792635346, 437.5319335124543, 439.8874670618925, 448.0221366776857, 451.7032900989036, 458.0682753336263, 477.39494908857966, 480.24820355724114, 499.78473861773705]
1562830503065
498.0 497.0 479.0 471.0 446.0 439.0 435.0 429.0 425.0 424.0 421.0 416.0 408.0 404.0 401.0 396.0 390.0 379.0 361.0 357.0 352.0 336.0 316.0 309.0 307.0 297.0 293.0 289.0 271.0 250.0 249.0 245.0 240.0 238.0 217.0 209.0 202.0 195.0 190.0 175.0 165.0 162.0 150.0 132.0 123.0 120.0 119.0 98.0 95.0 90.0 84.0 79.0 64.0 31.0 36.0 43.0 76.0 94.0 98.0 112.0 118.0 124.0 125.0 141.0 174.0 187.0 191.0 197.0 227.0 234.0 235.0 236.0 237.0 268.0 299.0 305.0 312.0 321.0 328.0 331.0 344.0 348.0 351.0 361.0 371.0 375.0 380.0 390.0 411.0 419.0 423.0 430.0 438.0 440.0 449.0 452.0 459.0 478.0 481.0 500.0 
1562830503068
497.0 496.0 478.0 470.0 445.0 438.0 434.0 428.0 424.0 423.0 420.0 415.0 407.0 403.0 400.0 395.0 389.0 378.0 360.0 356.0 351.0 335.0 315.0 308.0 306.0 296.0 292.0 288.0 270.0 249.0 248.0 244.0 239.0 237.0 216.0 208.0 201.0 194.0 189.0 174.0 164.0 161.0 149.0 131.0 122.0 119.0 118.0 97.0 94.0 89.0 83.0 78.0 63.0 30.0 35.0 42.0 75.0 93.0 97.0 111.0 117.0 123.0 124.0 140.0 173.0 186.0 190.0 196.0 226.0 233.0 234.0 235.0 236.0 267.0 298.0 304.0 311.0 320.0 327.0 330.0 343.0 347.0 350.0 360.0 370.0 374.0 379.0 389.0 410.0 418.0 422.0 429.0 437.0 439.0 448.0 451.0 458.0 477.0 480.0 499.0 
1562830503071
[31.0, 35.0, 43.0, 64.0, 76.0, 79.0, 84.0, 89.0, 94.0, 95.0, 97.0, 98.0, 111.0, 118.0, 119.0, 119.0, 123.0, 124.0, 124.0, 132.0, 140.0, 149.0, 161.0, 165.0, 174.0, 174.0, 186.0, 189.0, 190.0, 194.0, 197.0, 202.0, 209.0, 216.0, 226.0, 234.0, 234.0, 235.0, 236.0, 238.0, 240.0, 244.0, 248.0, 249.0, 268.0, 271.0, 288.0, 293.0, 297.0, 299.0, 305.0, 307.0, 308.0, 312.0, 315.0, 320.0, 327.0, 330.0, 336.0, 343.0, 347.0, 351.0, 352.0, 357.0, 361.0, 361.0, 371.0, 375.0, 379.0, 379.0, 389.0, 390.0, 395.0, 400.0, 403.0, 407.0, 411.0, 415.0, 419.0, 420.0, 422.0, 423.0, 425.0, 428.0, 429.0, 435.0, 438.0, 438.0, 440.0, 446.0, 448.0, 452.0, 458.0, 471.0, 477.0, 479.0, 480.0, 497.0, 498.0, 500.0]
1562830503071
input
95
9
[31.0, 35.0, 43.0, 64.0, 76.0, 79.0, 84.0, 89.0, 94.0, 97.0, 98.0, 111.0, 118.0, 119.0, 119.0, 123.0, 124.0, 124.0, 132.0, 140.0, 149.0, 161.0, 165.0, 174.0, 174.0, 186.0, 189.0, 190.0, 194.0, 197.0, 202.0, 209.0, 216.0, 226.0, 234.0, 234.0, 235.0, 236.0, 238.0, 240.0, 244.0, 248.0, 249.0, 268.0, 271.0, 288.0, 293.0, 297.0, 299.0, 305.0, 307.0, 308.0, 312.0, 315.0, 320.0, 327.0, 330.0, 336.0, 343.0, 347.0, 351.0, 352.0, 357.0, 361.0, 361.0, 371.0, 375.0, 379.0, 379.0, 389.0, 390.0, 395.0, 400.0, 403.0, 407.0, 411.0, 415.0, 419.0, 420.0, 422.0, 423.0, 425.0, 428.0, 429.0, 435.0, 438.0, 438.0, 440.0, 446.0, 448.0, 452.0, 458.0, 471.0, 477.0, 479.0, 480.0, 497.0, 498.0, 500.0]
add
10
[31.0, 35.0, 43.0, 64.0, 76.0, 79.0, 84.0, 89.0, 94.0, 97.0, 583.0, 98.0, 111.0, 118.0, 119.0, 119.0, 123.0, 124.0, 124.0, 132.0, 140.0, 149.0, 161.0, 165.0, 174.0, 174.0, 186.0, 189.0, 190.0, 194.0, 197.0, 202.0, 209.0, 216.0, 226.0, 234.0, 234.0, 235.0, 236.0, 238.0, 240.0, 244.0, 248.0, 249.0, 268.0, 271.0, 288.0, 293.0, 297.0, 299.0, 305.0, 307.0, 308.0, 312.0, 315.0, 320.0, 327.0, 330.0, 336.0, 343.0, 347.0, 351.0, 352.0, 357.0, 361.0, 361.0, 371.0, 375.0, 379.0, 379.0, 389.0, 390.0, 395.0, 400.0, 403.0, 407.0, 411.0, 415.0, 419.0, 420.0, 422.0, 423.0, 425.0, 428.0, 429.0, 435.0, 438.0, 438.0, 440.0, 446.0, 448.0, 452.0, 458.0, 471.0, 477.0, 479.0, 480.0, 497.0, 498.0, 500.0]

Topics: Java