小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

System類常用方法講解(Java基礎)

 醉人說夢 2023-04-10 發(fā)布于廣東

【辰兮要努力】:hello你好我是辰兮,很高興你能來閱讀,昵稱是希望自己能不斷精進,向著優(yōu)秀程序員前行!

博客來源于項目以及編程中遇到的問題總結,偶爾會有讀書分享,我會陸續(xù)更新Java前端、后臺、數(shù)據(jù)庫、項目案例等相關知識點總結,感謝你的閱讀和關注,希望我的博客能幫助到更多的人,分享獲取新知,大家一起進步!

吾等采石之人,應懷大教堂之心,愿你們奔赴在各自的熱愛中…

System類是我們最常接觸的類之一,但是我們常常忽略,趁有時間整理一波相關基礎。

還記得自己第一篇上熱門的文章就是:秋招Java-面試官就System.out.println()考了我半個小時?感興趣的回去打卡哦

在這里插入圖片描述

System類

java.lang.System類中提供了大量的靜態(tài)方法,可以獲取與系統(tǒng)相關的信息或系統(tǒng)級操作,在System類的API文檔中,常用的方法有:

  • public static long currentTimeMillis():返回以毫秒為單位的當前時間。
  • public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):將數(shù)組中指定的數(shù)據(jù)拷貝到另一個數(shù)組中。

1.1 currentTimeMillis方法

實際上,currentTimeMillis方法就是 獲取當前系統(tǒng)時間與1970年01月01日00:00點之間的毫秒差值

import java.util.Date;

public class SystemDemo {
    public static void main(String[] args) {
       	//獲取當前時間毫秒值
        System.out.println(System.currentTimeMillis()); // 1916090537145
    }
}

案例:驗證for循環(huán)打印數(shù)字1-9999所需要使用的時間(毫秒)

public class SystemTest {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("共耗時毫秒:" + (end - start));
    }
}

運行結果

共耗時毫秒:110

項目測試實踐案例:

之前做社交圈項目,涉及到一個評論檢測:意思是你發(fā)送信息,要進行安全檢測,即不能發(fā)不符合規(guī)范的言論,這個步驟會先調(diào)用微信的接口,檢測成功后執(zhí)行后續(xù)步驟

評論檢測涉及到免費檢測 和 付費檢測,付費檢測更加全面,當然花費時間也較多,被反饋評論這個過慢后測試了一下兩個不同檢測方法的速度。

這里就是使用System.currentTimeMillis()這個方法進行計算評論檢測的時間

如免費檢測時間1660毫秒
在這里插入圖片描述
付費檢測時間2690毫秒
在這里插入圖片描述
所以小結下來確實是調(diào)用不同的檢測內(nèi)容接口導致速度變慢,可能檢測更加詳細所以時間花費較多。


1.2 arraycopy方法

  • public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length):將數(shù)組中指定的數(shù)據(jù)拷貝到另一個數(shù)組中。

數(shù)組的拷貝動作是系統(tǒng)級的,性能很高。System.arraycopy方法具有5個參數(shù),含義分別為:

參數(shù)序號參數(shù)名稱參數(shù)類型參數(shù)含義
1srcObject源數(shù)組
2srcPosint源數(shù)組索引起始位置
3destObject目標數(shù)組
4destPosint目標數(shù)組索引起始位置
5lengthint復制元素個數(shù)

實踐案例

將src數(shù)組中前3個元素,復制到dest數(shù)組的前3個位置上復制元素前:src數(shù)組元素[1,2,3,4,5],dest數(shù)組元素[6,7,8,9,10]復制元素后:src數(shù)組元素[1,2,3,4,5],dest數(shù)組元素[1,2,3,9,10]

import java.util.Arrays;

public class DemoSystemArrayCopy {

    public static void main(String[] args) {
        int[] src = new int[]{1,2,3,4,5};
        int[] dest = new int[]{6,7,8,9,10};
        System.arraycopy( src, 0, dest, 0, 3);
        int[] arr = dest;
        System.out.println(Arrays.toString(arr));
        /*代碼運行后:兩個數(shù)組中的元素發(fā)生了變化
         src數(shù)組元素[1,2,3,4,5]
         dest數(shù)組元素[1,2,3,9,10]
        */
        int [] array1 = { 1, 2, 3, 4, 5 };
        int [] array2 = { 4, 5, 6, 7, 8 };
        int [] array3 =  new  int [8];
        System.arraycopy (array1, 0, array3, 0, 5);
        //直接打印是打印不出來的要用數(shù)組的打印方式
        System.out.println(Arrays.toString(array3));
        System.arraycopy (array2, 2, array3, 5, 3);
        System.out.println(Arrays.toString(array3));

        //從舊數(shù)組拷貝到新數(shù)組
        int [] arr1 = { 1, 2, 3, 4, 5 };
        int [] arr2 = new int[5];
        //從舊數(shù)組拷貝到新數(shù)組
        //原來可以使用for循環(huán)遍歷拷貝
//        for (int i=0;i<arr1.length;i++){
//            arr2[i]=arr1[i];
//        }
        System.arraycopy(arr1, 0, arr2, 0, arr1.length);
        System.out.println("從舊數(shù)組拷貝到新數(shù)組"+ Arrays.toString(arr2));
    }
}

輸出結果

[1, 2, 3, 9, 10]
[1, 2, 3, 4, 5, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8]
從舊數(shù)組拷貝到新數(shù)組[1, 2, 3, 4, 5]

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多