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

Wesley13
• 阅读 656

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 }

 结果不唯一:

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

[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

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这