java.util.Arrays,java.lang.Math,java.lang.System 类的常用方法汇总

Roy 2019-07-12 01:4619 阅读

java.util.Arrays类是数组的工具类,一般数组常用的方法包括

二分查找:public static int  binarySearch(array[],int key),返回key的下标index

扩容缩容:public static int[]  copyOf(array[],newLength),返回新数组

取部分:public static int[]  copyOfRange(array[],fromindex,toindex)   ,注意[from,to)是开区间,返回新数组

数组从小到大快速排序:public static void Arrays.sort(array[]),

整体赋值:public static void fill(array[],value), 

输出为字符串:public static String  toString(array[]),返回字符串

检查是否相等:public static boolean  equals(array1[],array2[]),返回布尔值

 

java.lang.Math类是常用的数学类,常用的方法有:

取绝对值:public static int abs(int value),返回绝对值

向上取整:public static int ceil(double value),

向下取整:public static int  floor(double value);

四舍五入:public static long round(double value);

[0,1)de随机数:public static double  random();

 

java.lang.System类有很多常用系统函数:

取当前系统时间距离1970年1月1日0点的毫秒数:public static long  currentTimeMillis()

数组复制粘贴:public static  void  arrayCopy(Object src, int srcPos, Object dest, int destPos, int length):

  * src:源数组

  * srcPos:源数组的起始下标

  * dest:目标数组

  * destPos:目标数组的起始下标

  * src[srcPos]元素放到dest[destPos]

  * 依次存放

  * length:一共要复制几个

  *
  * 如果src和dest是同一个数组时,当[srcPos]>[destPos]时,从后往前移动,一般用于元素的删除
  * 如果src和dest是同一个数组时,当[srcPos]<[destPos]时,从前往后移动,

 

提供一个自己加深巩固印象的例题:

 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));//将数组转换为字符串并输出
15         double[] array1=new double[array.length];
16         array1=Arrays.copyOf(array, array.length);//复制整个数组长度给array1
17         Arrays.sort(array);//对数组进行快排
18         System.out.println();
19         System.out.print(Arrays.toString(array));//输出快排后的数组
20         System.out.println();
21         System.out.println(Arrays.equals(array, array1));//判断快排后的数组和快排前的数组是否相等
22         System.out.println(Arrays.toString(Arrays.copyOfRange(array, 0, 10)));//复制从下标0开始的10个元素返回数组并转换为字符串输出
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);//将数组全部元素赋值为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]);//数组元素取绝对值
30         }
31         System.out.println(Arrays.toString(array));
32         System.out.println(System.currentTimeMillis());//输出当前系统时间,距离1970.1.1 0:0:0的毫秒数
33         
34         for(int i=0;i<=array.length-1;i++) {
35             System.out.print(Math.ceil(array[i])+" ");//向上取整
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])+" ");//向下取整
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]);//四舍五入
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);//二分查找值为key的下标
57         System.out.println(index);
58         System.arraycopy(array, index+1, array, index, array.length-index-1);//覆盖掉key
59         array=Arrays.copyOf(array, array.length-1);//数组缩容
60         System.out.println(Arrays.toString(array));    
61 
62         //在key1处增加一个随机元素
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从下标key1复制到最后一个下标,注意是开区间所以要加一个
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 }

 结果不唯一:

[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]
View Code

 

回复数量: 0
暂无评论~~
  请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!