【问题标题】:pass by reference an integer in java [duplicate]在java中通过引用传递一个整数[重复]
【发布时间】:2020-06-11 12:59:28
【问题描述】:

我需要在 java 中通过引用传递一个整数。有没有一种简单的方法可以做到这一点?在 C++ 中,通过在整数之前放置“&”将通过引用传递。 这是我试图转向 Java 的 C 代码:

void count(int distance, int i, int &counter, int array[], int n) {
    if (i == distance) 
        counter++;
    else {
        for (int j = 0; j < n; j++) {
            if (i <= distance - array[j]) 
                count(distance, i + array[j], counter, array, n);
        }
    }
}

有没有办法在没有整数对象的情况下这样做? (我不想再上课了)

【问题讨论】:

标签: java c++


【解决方案1】:

一个不同的(功能性)解决方案,传递一个Runnable 进行计数(或任何需要的):

void count(int distance, int i, Runnable counter, int array[], int n) {
    if (i == distance) {
        counter.run();
    }

这将被称为:

private int count;  // does not work with local variable
    // ...
    count(distance, i, ()->count++, array, n);

或者,使用方法:

    count(distance, i, this::increment, array, n);
    // ...
private void increment() {
    count++;
}

【讨论】:

    【解决方案2】:

    您可能需要让您的方法返回计数器

    我不确定这是不是同一个算法,但这只是我的想法:

    public static void main(String[] args) {
        int counter = 3;
        counter = count(2, 1, counter, new int[] {1,2,3}, 3);
        System.out.println(counter);
    }
    
    static int count(int distance, int i, int counter, int array[], int n) {
        if (i == distance) {
            counter++;
        } else {
            for (int j = 0; j < n; j++) {
                if (i <= distance - array[j])
                    counter = count(distance, i + array[j], counter, array, n);
            }
        }
        return counter;
    }
    

    【讨论】:

    • 我真的不认为你想做counter += count(...)。我想你只想要=
    • @AndyTurner 老实说我不知道​​。
    • @AndyTurner 好的,我设法运行 C++ 代码进行比较,你是对的,应该是 =
    【解决方案3】:

    您将需要一个对象,但您不必自己构建它。 作为Andy Turner said,您可以使用int 数组或AtomicInteger 所以:

    int[] counter = new int[]{0};
    counter[0]++;
    

    ..

    AtomicInteger counter = new AtomicInteger();
    counter.incrementAndGet();
    

    您可以在commons-lang package 中使用MutableInt

    MutableInt counter = new MutableInt();
    counter.increment();
    

    【讨论】:

      【解决方案4】:

      如您所知,Java 是原始数据类型的按值传递(即使对于像整数这样的包装器) 一种方式 - 传递一个以 i 作为实例成员的类对象。 另一种方法 - 将 i 作为类的实例成员,不要将 i 作为方法参数传递。

      【讨论】:

        【解决方案5】:

        在 java 中,原始变量总是与“按值传递”绑定。如果要实现按引用传递功能,则需要在对象的帮助下传递这些值。您可以查看此链接中的示例:- Call by Value and Call by Reference in Java

        【讨论】:

          【解决方案6】:

          can't pass by reference in Java

          您可以传入一个可变容器,例如 int[1]AtomicInteger

          void count(int distance, int i, int[] counter, int array[], int n)
          

          或者您可以使用返回值来返回 counter 的更新值:

          int count(int distance, int i, int array[], int n) {
            if (i == distance) return 1;
            else {
                int counter = 0;
                for (int j = 0; j < n; j++) {
                    if (i <= distance - array[j]) counter += count(distance, i + array[j], array, n);
                }
                return counter;
            }
          }
          

          【讨论】:

          • 值得指出的是,整数是不可变的,这就是为什么你不能简单地传递它。
          【解决方案7】:

          在 Java 中通过引用传递原始类型的唯一方法是将其包装在一个对象中。从根本上说,您不能通过引用传递原始类型,因为它们不是面向对象的。

          查看这篇文章了解更多信息:How do I pass a primitive data type by reference?

          【讨论】:

            猜你喜欢
            • 2013-05-16
            • 1970-01-01
            • 2011-08-28
            • 2013-10-31
            • 2011-06-03
            • 1970-01-01
            • 2010-12-31
            • 2017-03-21
            • 2011-02-24
            相关资源
            最近更新 更多