jayden orr jayden orr - 24 days ago 6
Java Question

How to make a Fibonacci series dynamic array java program

I am beginning java and I am trying to make a Fibonacci series with a dynamic array. In my class I was given a Fibonacci series program, and a example of a dynamic array. I am really confused. Can someone show me what this looks like?

Answer

try this,

import java.util.*;
public class fib{
    public static void main(String[] args){
        ArrayList<Integer> arr = new ArrayList<Integer>();//ArrayList
        int i = 0, j =1;
        System.out.print(i +" " +j + " ");
        //generate 10 fibonacci numbers
        int s = 0;
        while(s < 10){
            int temp = i + j;
            arr.add(temp);
            i = j;
            j = temp;
            s++;
        }

        for(Integer v : arr)
            System.out.print(v+" ");
    }
}