模拟Java中数组引用(即对引用的引用)的引用传递
我想知道,在java中,是否可以无论如何模拟数组的引用传递?是的,我知道该语言不支持它,但无论如何我可以做到这一点。举例来说,我想创建一个方法来反转数组中所有元素的顺序。 (我知道这个代码片段不是最好的例子,因为有更好的算法可以做到这一点,但这是我想要为更复杂的问题做的事情类型的一个很好的例子)。
目前,我需要创建一个这样的类:
public static void reverse(Object[] arr) {
Object[] tmpArr = new Object[arr.length];
count = arr.length - 1;
for(Object i : arr)
tmpArr[count--] = i;
// I would like to do arr = tmpArr, but that will only make the shallow
// reference tmpArr, I would like to actually change the pointer they passed in
// Not just the values in the array, so I have to do this:
for(Object i : tmpArr)
arr[count++] = i;
return;
}
是的,我知道我可以交换值直到到达中间,这样会更有效,但是对于其他更复杂的目的,我是否可以这样做可以操纵实际的指针吗?
再次感谢您。
I was wondering, in java, is it possible to in anyway, simulate pass by reference for an array? Yes, I know the language doesn't support it, but is there anyway I can do it. Say, for example, I want to create a method that reverses the order of all the elements in an array. (I know that this code snippet isn't the best example, as there is a better algorithms to do this, but this is a good example of the type of thing I want to do for more complex problems).
Currently, I need to make a class like this:
public static void reverse(Object[] arr) {
Object[] tmpArr = new Object[arr.length];
count = arr.length - 1;
for(Object i : arr)
tmpArr[count--] = i;
// I would like to do arr = tmpArr, but that will only make the shallow
// reference tmpArr, I would like to actually change the pointer they passed in
// Not just the values in the array, so I have to do this:
for(Object i : tmpArr)
arr[count++] = i;
return;
}
Yes, I know that I could just swap the values until I get to the middle, and it would be much more efficient, but for other, more complex purposes, is there anyway that I can manipulate the actual pointer?
Again, thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java不按引用传递,因此不能直接操作原始指针。正如您所发现的,Java 按值传递所有内容。 您不能传递对数组对象的引用,并期望有方法修改原始引用以指向另一个数组对象。
当然,您可以:
java.util.Arrays.sort
)Throwable
有一个setStackTrace
)返回
新引用(alajava.util.Arrays.copyOf
)Java does not pass by reference, so you can't directly manipulate the original pointer. As you've found out, Java passes everything by value. You can't pass a reference to an array object, and expect a method to modify the original reference to point to another array object.
You can, of course:
java.util.Arrays.sort
)Throwable
has asetStackTrace
)return
the new reference instead (alajava.util.Arrays.copyOf
)那么,您可以显式传递包含引用的对象。
java.util.concurrent.atomic.AtomicReference
开箱即用,尽管它确实带有您可能不想要的易失性语义。有些人使用单元素数组从匿名内部类返回值(尽管这对我来说似乎不是一个好主意)。Well, you can explicitly pass an object that contains a reference.
java.util.concurrent.atomic.AtomicReference
is ready out of the box, although it does come with volatile semantics that you probably don't want. Some people use single element arrays to returns values from anonymous inner classes (although that doesn't seem a great idea to me).此方法将数组的元素反转到位。调用者会看到更改。 (在 Java 中,一切都是按值传递的,包括对象引用。)
This method reverses the Array's elements in place. The caller sees the changes. (In Java everything is passed by value, including object references.)
在 Java 中,对象引用是按值传递的。
因此,如果您正在寻找类似 Then 的内容
,那么它在 Java 中不可能通过任何直接方式实现。
如果我们只需要更改存储在数组中的值,那么我们可以做到这一点,因为对象引用是按值传递的。
In Java Object reference is passed by value.
So if you looking for something like
Then its not possible in Java by any direct means.
If we need to change only the values stored in an array, then we can do it since object reference is passed by value.
您想要传递对数组引用的引用。在这种情况下,您只需创建一个类来保存引用并将引用传递给该类,或者只传递所传递类型的 1 元素数组。然后,您将传递一个保存数组的对象或一个其唯一元素包含您要操作的数组的数组。
You want to pass a reference to the array reference. In that case you just have to either create a class to hold the reference and pass a reference to that class or just pass a 1-element array of the type being passed. Then you'd be passing either an object holding the array or an array whose only element contains the array you want to operate on.