Java基础 第三节 第三课
【摘要】
System 类
概述currentTimeMillis 方法练习
arraycopy 方法练习
概述
java.lang.System类中提供了大量的静态方法, 可以获取与系统相...
概述
java.lang.System类中提供了大量的静态方法, 可以获取与系统相关的信息或系统级操作. 在 System 类的 API 文档中, 常用的方法有:
public static long currentTimeMills(): 返回以毫秒为单位的当前时间public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length): 将数组中指定的数据拷贝到另一个数组中
currentTimeMillis 方法
实际上, currentTImeMillis 方法就是获取当前系统时间与 1970 年 01 月 01 日 00:00 点之间的毫秒差值.
public class Test14 {
public static void main(String[] args) {
// 获取当前时间毫秒值
System.out.println(System.currentTimeMillis()); // 输出结果: 1606453054627
}
}
- 1
- 2
- 3
- 4
- 5
- 6
练习
验证 for 循环打印数字 1-9999 所需要使用的时间. (毫秒)
public class Test15 {
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) + " 毫秒");
}
}
输出结果:
总共耗时: 602 毫秒
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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 个位置上复制元素前: scr 数组元素 [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 Test16 {
public static void main(String[] args) {
// 定义src, dest数组
int[] src = {1,2,3,4,5};
int[] dest = {6,7,8,9,10};
System.out.println("复制元素前");
System.out.println(Arrays.toString(src));
System.out.println(Arrays.toString(dest));
// 调用arraycopy方法
System.arraycopy(src,0,dest,0,3);
System.out.println("复制元素后");
System.out.println(Arrays.toString(src));
System.out.println(Arrays.toString(dest));
}
}
输出结果:
复制元素前
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
复制元素后
[1, 2, 3, 4, 5]
[1, 2, 3, 9, 10]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
文章来源: iamarookie.blog.csdn.net,作者:我是小白呀,版权归原作者所有,如需转载,请联系作者。
原文链接:iamarookie.blog.csdn.net/article/details/110227645
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)