模拟Java中数组引用(即对引用的引用)的引用传递

发布于 2024-08-25 12:55:53 字数 748 浏览 7 评论 0原文

我想知道,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怕倦 2024-09-01 12:55:53

我可以操纵实际的指针吗?

Java不按引用传递,因此不能直接操作原始指针。正如您所发现的,Java 按值传递所有内容。 您不能传递对数组对象的引用,并期望有方法修改原始引用以指向另一个数组对象

当然,您可以:

  • 修改引用的数组对象的元素(ala java.util.Arrays.sort
  • 传递对具有可设置字段的对象的引用(例如 Throwable有一个setStackTrace
  • 返回新引用(ala java.util.Arrays.copyOf

is there anyway that I can manipulate the actual pointer?

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:

  • Modify elements of the referred array object (ala java.util.Arrays.sort)
  • Pass a reference to an object with a settable field (e.g. Throwable has a setStackTrace)
  • return the new reference instead (ala java.util.Arrays.copyOf)
冰之心 2024-09-01 12:55:53

那么,您可以显式传递包含引用的对象。 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).

丶视觉 2024-09-01 12:55:53

此方法将数组的元素反转到位。调用者会看到更改。 (在 Java 中,一切都是按值传递的,包括对象引用。)

   public static void reverse(Object[] arr) {
       for ( int i = 0, j = arr.length - 1;   i < j;   i++, j-- ) {
           Object temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
       }
   }

This method reverses the Array's elements in place. The caller sees the changes. (In Java everything is passed by value, including object references.)

   public static void reverse(Object[] arr) {
       for ( int i = 0, j = arr.length - 1;   i < j;   i++, j-- ) {
           Object temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
       }
   }
方圜几里 2024-09-01 12:55:53

在 Java 中,对象引用是按值传递的。

因此,如果您正在寻找类似 Then 的内容

function referenceCheck()
{
    int[] array = new int[]{10, 20, 30};
    reassignArray(&array);
    //Now array should contain 1,2,3,4,5
}

function reassignArray(int **array)
{
    int *array = new int[] { 1, 2, 3, 4, 5};
}

,那么它在 Java 中不可能通过任何直接方式实现。

如果我们只需要更改存储在数组中的值,那么我们可以做到这一点,因为对象引用是按值传递的。

In Java Object reference is passed by value.

So if you looking for something like

function referenceCheck()
{
    int[] array = new int[]{10, 20, 30};
    reassignArray(&array);
    //Now array should contain 1,2,3,4,5
}

function reassignArray(int **array)
{
    int *array = new int[] { 1, 2, 3, 4, 5};
}

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.

夜吻♂芭芘 2024-09-01 12:55:53

您想要传递对数组引用的引用。在这种情况下,您只需创建一个类来保存引用并将引用传递给该类,或者只传递所传递类型的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文