Java 如何将数组的大小加倍
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22252450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to double the size of an array
提问by Jason
I have to implement an array that takes its elements from a random generator. Whenever the array reaches it's last element a method resize() will be called to increase the size of the array. In my code every thing goes alight until invoking resize() method, it does not do any effect on the size of the array. It supposed to increase the size of the array to allow more elements to be entered.
我必须实现一个从随机生成器中获取其元素的数组。每当数组到达它的最后一个元素时,就会调用一个方法 resize() 来增加数组的大小。在我的代码中,在调用 resize() 方法之前,一切都会发生,它对数组的大小没有任何影响。它应该增加数组的大小以允许输入更多元素。
for (int j=0; j<arr.length-1; j++) {
random= generator.nextInt(arr.length*4) ;
arr[j]=random;
}
if(arr_size==arr.length){
resize(arr);
for (int h=0; h<(arr.length-1)*2; h++) {
random= generator.nextInt(arr.length*4) ;
arr[h]=random;}
}
Here is resize():
这是调整大小():
private static void resize(int[] arr) {
int[] tmp = new int[2*arr.length];
System.arraycopy(arr,0,tmp,0,arr.length);
arr = tmp;
}
采纳答案by Starscream1984
Either set and return the tmp variable as described by Kon, or you pass in arr
by reference to make changes to it persist outside of the resize function:
要么按照 Kon 的描述设置并返回 tmp 变量,要么arr
通过引用传入以使对其的更改在调整大小函数之外保持不变:
private static void resize(ref int[] arr) { .... }
and call it like
并称之为
resize(ref arr);
回答by Kon
Setting arr = tmp
does not do what you think it does. You're simply pointing the local variable arr
in the resize()
method to the local variable tmp
. What you want to do is return tmp
and assign it to arr
outside of the method.
设置arr = tmp
不会做你认为它会做的事情。您只是arr
将resize()
方法中的局部变量指向局部变量tmp
。您想要做的是返回tmp
并将其分配给arr
方法之外。
if(arr_size==arr.length){
arr = resize(arr);
And change the resize()
method signature to
并将resize()
方法签名更改为
private static int[] resize(int[] arr) {
//Resize
return tmp;
}
The key thing to take away is that: When passing an object reference to a method in Java, you're passing a copy of that object's reference, which can be thought of as a copy of the location in memory of that object. All such manipulations to this REFERENCE in the called method won't take effect outside of the method. However, you CAN manipulate the actual object itself because, as I said, the reference is pointing to the same place in memory with the exact same object.
要带走的关键是:在 Java 中将对象引用传递给方法时,您传递的是该对象引用的副本,可以将其视为该对象内存中位置的副本。在被调用方法中对此 REFERENCE 的所有此类操作都不会在该方法之外生效。但是,您可以操作实际对象本身,因为正如我所说,引用指向内存中具有完全相同对象的相同位置。
But changing a copy of a location in memory to point to a new location in memory does not cause the original calling object reference to point to that same location
但是将内存中某个位置的副本更改为指向内存中的新位置不会导致原始调用对象引用指向同一位置
回答by Kumar
When you say New int, reference is changed. Java uses Copy by Reference not exactly Pass by reference, if you change reference by new the caller method properties wont have any change, in your case have a return type and assign it back to arr.
当您说 New int 时,引用已更改。Java使用按引用复制而不是完全按引用传递,如果您通过新更改引用,则调用方方法属性不会有任何更改,在您的情况下有返回类型并将其分配回arr。
回答by mok
try this :
尝试这个 :
private static int[] resize(int[] arr) {
int[] tmp = new int[2*arr.length];
System.arraycopy(tmp,0,arr,0,arr.length);
System.out.println(tmp.length);
return tmp;
}