我的印象是,在为数组指定的上限之外存储和访问数据将导致异常,但我正在编写解决方案,偶然发现了数组存储的元素比上限更多的场景。
public static void main(String[] args) {
int[][] arr = new int[1][5];
String values = "1 2 3 4 5 6";
arr[0] = Arrays.asList(values.split(" ")).stream().mapToInt(Integer::parseInt).toArray();
for (int i : arr[0]) {
System.out.println(i);
}
}上面的程序以不同的行输出1 2 3 4 5 6,显然我已经将我的上限指定为5。有人能解释一下这个奇怪的现象吗?
发布于 2019-09-22 02:12:17
赋值后,arr[0]持有一个list对象,该对象的边界设置为Arrays.asList定义的任何内容。
https://stackoverflow.com/questions/58045487
复制相似问题