Fibonacci recursion problem, cannot return 2 elements

I am having problems with the code in an else statement. I can’t figure out how to make recursively return 2 adjusted items in a list. I would appreciate any help.

public static int[] fibaux(int n) {

      if (n == 1) {
         return new int[] {1, 0};
      }
      else {
         int[] array = new int[2];
         list[] = {fibaux(n - 1)};


         return //array[0] + array[1], array[1];
      }
 }
+4
source share
2 answers

In Java, multi-valued values ​​are not allowed, i.e. you cannot return 2 values ​​with return. You can simply return arraycontaining two values, for example:

return new int[] {num1, num2};
+1
source

, , , . , , , , .

, :

public static int[] fibaux(int n) {
      if (n == 1) {
         return new int[] {1, 0};
      }
      else {
         int[] array = fibaux(n - 1);
         return new int[] {array[0]+array[1], array[0]};
      }
}
+1

Source: https://habr.com/ru/post/1628430/


All Articles