This might be a silly question but does a Java array accept more than its size? If yes then why do we need ArrayList? I thought that arrays had a fixed size that can't be increased at runtime. Here is my test code:
这可能是一个愚蠢的问题,但Java数组是否接受超过其大小?如果是,那么为什么我们需要ArrayList?我认为数组有一个固定的大小,在运行时无法增加。这是我的测试代码:
public class ArraySizeDemo {
int[] anArray = new int[5];
public int[] getAnArray() {
return anArray;
}
public void setAnArray(int[] anArray) {
this.anArray = anArray;
}
}
public class ArrayDemo {
public static void main(String[] args) {
ArraySizeDemo ar = new ArraySizeDemo();
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int testarray[] = new int[10];
ar.setAnArray(arr); // it should give an error here since I am trying to
// assign an array of 10 to an array of 5
testarray = ar.getAnArray();
for (int i = 0; i < arr.length; i++)
System.out.println(testarray[i]);
}
}
4 个解决方案
#1
17
The assignment
分配
this.anArray = anArray;
doesn't copy the elements of anArray
to this.anArray
.
不会将anArray的元素复制到this.anArray。
It changes the value of the this.anArray
variable to refer to the same array referenced by anArray
. Therefore, before the assignment this.anArray
refers to an array of length 5, and after the assignment it refers to a different array object of length 10.
它更改this.anArray变量的值以引用anArray引用的相同数组。因此,在赋值之前this.anArray引用长度为5的数组,并且在赋值之后它引用长度为10的不同数组对象。
If instead of this assignment you attempted to copy elements of the source array to the (smaller) target array, an exception would have been thrown, since the length of an array cannot be changed, so the elements of an array of length 10 cannot fit in an array of length 5.
如果您尝试将源数组的元素复制到(较小的)目标数组而不是此赋值,则会抛出异常,因为无法更改数组的长度,因此长度为10的数组的元素不适合长度为5的数组。
#2
6
You are changing the reference of this.anArray
doing this.anArray = anArray;
.
你正在改变this.anArray做这个的引用.anArray = anArray;。
After that assignment, this.anArray
is a reference pointing to another array, the one of ten elements.
在该赋值之后,this.anArray是指向另一个数组的引用,该数组是十个元素之一。
#3
6
You're not changing the array of 5. You are replacing it entirely.
你没有改变5的数组。你正在完全取代它。
If your method looked like this, it would result in the error you expect:
如果您的方法看起来像这样,则会导致您期望的错误:
public void setAnArray(int[] anArray) {
for (int i = 0; i < anArray.length; i++) {
this.anArray[i] = anArray[i];
}
}
However, after you do it the following way, the array of 5 is literally thrown away, and replaced with the array of 10:
但是,按照以下方式执行操作后,5的数组将被逐字丢弃,并替换为10的数组:
public void setAnArray(int[] anArray) {
this.anArray = anArray;
}
#4
0
when you instate object of ArraySizeDemo
=> ArraySizeDemo ar = new ArraySizeDemo();
what happened is something like this
当你instate对象的ArraySizeDemo => ArraySizeDemo ar = new ArraySizeDemo();发生的事情是这样的
Memory :
-------------------------
...
anArray point to 0x100
...
-------------------------
and when create another array int arr[] = {0,1,2,3,4,5,6,7,8,9};
当创建另一个数组时,int arr [] = {0,1,2,3,4,5,6,7,8,9};
Memory :
-------------------------
...
anArray point to 0x111
arr point to 0x222
...
-------------------------
and when call ar.setAnArray(arr)
you're passing the reference of arr
which is in are demo 0x222
当调用ar.setAnArray(arr)时,你传递的是arr的引用,它是demo 0x222
and in setAnArray()
body you're changing the reference of anArray
to the passed reference this.anArray = anArray;
并且在setAnArray()体中,您将anArray的引用更改为传递的引用this.anArray = anArray;
so after calling ar.setAnArray(arr)
所以在调用ar.setAnArray(arr)之后
Memory :
-------------------------
...
anArray point to 0x222
arr point to 0x222
...
-------------------------
#1
17
The assignment
分配
this.anArray = anArray;
doesn't copy the elements of anArray
to this.anArray
.
不会将anArray的元素复制到this.anArray。
It changes the value of the this.anArray
variable to refer to the same array referenced by anArray
. Therefore, before the assignment this.anArray
refers to an array of length 5, and after the assignment it refers to a different array object of length 10.
它更改this.anArray变量的值以引用anArray引用的相同数组。因此,在赋值之前this.anArray引用长度为5的数组,并且在赋值之后它引用长度为10的不同数组对象。
If instead of this assignment you attempted to copy elements of the source array to the (smaller) target array, an exception would have been thrown, since the length of an array cannot be changed, so the elements of an array of length 10 cannot fit in an array of length 5.
如果您尝试将源数组的元素复制到(较小的)目标数组而不是此赋值,则会抛出异常,因为无法更改数组的长度,因此长度为10的数组的元素不适合长度为5的数组。
#2
6
You are changing the reference of this.anArray
doing this.anArray = anArray;
.
你正在改变this.anArray做这个的引用.anArray = anArray;。
After that assignment, this.anArray
is a reference pointing to another array, the one of ten elements.
在该赋值之后,this.anArray是指向另一个数组的引用,该数组是十个元素之一。
#3
6
You're not changing the array of 5. You are replacing it entirely.
你没有改变5的数组。你正在完全取代它。
If your method looked like this, it would result in the error you expect:
如果您的方法看起来像这样,则会导致您期望的错误:
public void setAnArray(int[] anArray) {
for (int i = 0; i < anArray.length; i++) {
this.anArray[i] = anArray[i];
}
}
However, after you do it the following way, the array of 5 is literally thrown away, and replaced with the array of 10:
但是,按照以下方式执行操作后,5的数组将被逐字丢弃,并替换为10的数组:
public void setAnArray(int[] anArray) {
this.anArray = anArray;
}
#4
0
when you instate object of ArraySizeDemo
=> ArraySizeDemo ar = new ArraySizeDemo();
what happened is something like this
当你instate对象的ArraySizeDemo => ArraySizeDemo ar = new ArraySizeDemo();发生的事情是这样的
Memory :
-------------------------
...
anArray point to 0x100
...
-------------------------
and when create another array int arr[] = {0,1,2,3,4,5,6,7,8,9};
当创建另一个数组时,int arr [] = {0,1,2,3,4,5,6,7,8,9};
Memory :
-------------------------
...
anArray point to 0x111
arr point to 0x222
...
-------------------------
and when call ar.setAnArray(arr)
you're passing the reference of arr
which is in are demo 0x222
当调用ar.setAnArray(arr)时,你传递的是arr的引用,它是demo 0x222
and in setAnArray()
body you're changing the reference of anArray
to the passed reference this.anArray = anArray;
并且在setAnArray()体中,您将anArray的引用更改为传递的引用this.anArray = anArray;
so after calling ar.setAnArray(arr)
所以在调用ar.setAnArray(arr)之后
Memory :
-------------------------
...
anArray point to 0x222
arr point to 0x222
...
-------------------------