Can someone show me what I'm doing wrong this code? I'm writing fibonacci using an ArrayList | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can someone show me what I'm doing wrong this code? I'm writing fibonacci using an ArrayList

https://code.sololearn.com/caVF4hnap4Yr/?ref=app

28th Oct 2019, 12:55 AM
Ify Nonyelu
4 Answers
+ 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 :-)
28th Oct 2019, 3:18 AM
Rodrigo Oliveira
Rodrigo Oliveira - avatar
+ 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 }
28th Oct 2019, 4:44 PM
Denise Roßberg
Denise Roßberg - avatar
29th Oct 2019, 3:15 PM
Denise Roßberg
Denise Roßberg - avatar
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
29th Oct 2019, 5:20 AM
Ify Nonyelu