0
Can someone show me what I'm doing wrong this code? I'm writing fibonacci using an ArrayList
4 Respostas
+ 2
It seems you're copying code from different sources without understanding it. You define a method, but you never use it, then you add integers to a list and print them. Read about recursion first before trying that. Or if your goal is just to print the numbers, then keep doing what you did inside main method. Best of luck :-)
+ 2
Rec(fib) can't work because fib is an array list and Rec(int n) expects an integer.
Your method returns a fibonacci number at a specific position.
You can do this:
for(int i = 0; i <= 10; i++){
fib.add(Rec(i));
}
But you should learn recursion. If you add a print statement at the beginning of the method you will see what happens with n. But I think that you understand it if you are not familiar with recursion.
A simpler way: using a loop
fib.add(1);
fib.add(1);
And inside the loop you are adding the sum of the last two elements to fib.
The last element is fib.size() - 1.
while(fib.size() <= 10){
//your code
}
+ 1
Hope this helps you:
https://code.sololearn.com/cYOnwVjUBZ2p/?ref=app
0
Thank you for your advice, but I'm still confused though. Can I write a fibonacci code using ArrayList with Recursive? If so, can you show me how because I'm stuck