Understand the common methods of Java System classes in one minute

A large number of static methods are provided in the java.lang.System class to obtain system-related information or system-level operations


The java.lang.System class provides a large number of static methods to obtain system-related information or system-level operations. In the API documentation of the System class, the commonly used methods are:
public static long currentTimeMillis (): Returns in milliseconds The current time of the unit.

public static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length):
Copy the specified data in the array to another array.
*/

1. currentTimeMillis(): Returns the current time in milliseconds.

The code is as follows (example):
verify the time (milliseconds) required to print the number 1-9999 in the for loop

private static void currentTimeMillis()
    {
    
    
        long s = System.currentTimeMillis();
        //执行for循环
        for (int i = 0; i < 9999; i++) {
    
    
            System.out.println(i);

        }
        //执行程序后,再次获取一次毫秒值
        long e = System.currentTimeMillis();
        System.out.println("程序共耗时:"+(e-s)+"毫秒值");//120
    }

2. arraycopy() copies the specified data in the array to another array.

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):
Parameters:
src-source array.
srcPos-the starting position (starting index) in the source array.
dest-the destination array.
destPos-the starting position in the target data.
length-the number of array elements to be copied.
Exercise:
Copy the first 3 elements of the src array to the first 3 positions of the dest array Before
copying the elements:
src array element [1,2,3,4,5], dest array element [6,7,8, 9,10] After
copying the elements:
src array element [1,2,3,4,5], dest array element [1,2,3,9,10]

private  static  void arraycopy()
    {
    
    
    //第一步定义源数组
    int [] src = {
    
    1,2,3,4,5,6,};
    //第二步定义目标数组
    int [] dest= {
    
    6,7,8,9,10};
    System.out.println("复制前"+ Arrays.toString(dest));
    // 使用System类中的arraycopy把源数组的前3个元素复制到目标数组的前3个位置上
    System.arraycopy(src,0,dest,0,3);
    System.out.println("复制后"+Arrays.toString(dest));//复制后[1, 2, 3, 9, 10]

    }

Guess you like

Origin blog.csdn.net/weixin_46235428/article/details/109262078