Java 小知识点

2018-05-13  本文已影响0人  CodingSnail

1、打印数组

int[] result = {56,78,956,523};
System.out.println(Arrays.toString(result));
String[] names = {"zhangshan","Pony","Jack","lisi"};
System.out.println(Arrays.toString(names));

结果如下:
[56, 78, 956, 523]
[zhangshan, Pony, Jack, lisi]

2、数组copy
在Java中,允许将一个数组变量拷贝给另一个数组变量,这时这两个数组将引用同一个数组,如果长度小于原始数组的长度,则只拷贝最前面的数据元素。

int[] age = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[] age1 = Arrays.copyOf(age, 6);
System.out.println("age1=" + Arrays.toString(age1));
int[] age2 = Arrays.copyOf(age, 20);
System.out.println("age2=" + Arrays.toString(age2));

结果如下:               
age1=[1, 2, 3, 4, 5, 6]
age2=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
上一篇下一篇

猜你喜欢

热点阅读