java.lang.System
類中提供了大量的靜態方法,可以獲取與系統相關的信息或系統級操作,在System類的API文檔中,常用的方法有:
public static long currentTimeMillis()
:返回以毫秒爲單位的當前時間。public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
:將數組中指定的數據拷貝到另一個數組中。
currentTimeMillis方法
實際上,currentTimeMillis方法就是 獲取當前系統時間與1970年01月01日00:00點之間的毫秒差值。
import java.util.Date;
public class SystemDemo {
public static void main(String[] args) {
//獲取當前時間毫秒值
System.out.println(System.currentTimeMillis()); // 1516090531144
}
}
練習
驗證for循環打印數字1-9999所需要使用的時間(毫秒)
public class SystemTest1 {
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));
}
}
arraycopy方法
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
:將數組中指定的數據拷貝到另一個數組中。
數組的拷貝動作是系統級的,性能很高。System.arraycopy方法具有5個參數,含義分別爲:
參數序號 | 參數名稱 | 參數類型 | 參數含義 |
---|---|---|---|
1 | src | Object | 源數組 |
2 | srcPos | int | 源數組索引起始位置 |
3 | dest | Object | 目標數組 |
4 | destPos | int | 目標數組索引起始位置 |
5 | length | int | 複製元素個數 |
練習
將src數組中前3個元素,複製到dest數組的前3個位置上覆制元素前:src數組元素[1,2,3,4,5],dest數組元素[6,7,8,9,10]複製元素後:src數組元素[1,2,3,4,5],dest數組元素[1,2,3,9,10]。
import java.util.Arrays;
public class Demo11SystemArrayCopy {
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);
/*代碼運行後:兩個數組中的元素髮生了變化
src數組元素[1,2,3,4,5]
dest數組元素[1,2,3,9,10]
*/
}
}
我是知識的搬運工,該篇文章內容藉助於高端IT教育品牌——黑馬程序員