2

I need to turn a code that prints out the Fibonacci series that also uses dynamic arrays, here is what I have so far, I just don't know how to turn it into a dynamic array.

public class Fibonacci {
    public static void main(String[] args) {
        int[] numbers;
        numbers = new int[20];
        numbers[0] = 1;
        numbers[1] = 1;
        System.out.println("\nFibonacci series:\n");
        System.out.println(numbers[0]);
        System.out.println(numbers[1]);
        for (int i = 2; i < 20; i++) {
            numbers[i] = numbers[i-2]+numbers[i-1];
            System.out.println(numbers[i]);
        }
    }
}
3
  • Do you actually need to dynamically resize an array, or are you allowed to just use an ArrayList? Commented Apr 20, 2015 at 18:33
  • s/int[]/List<Integer>/g. Done! Commented Apr 20, 2015 at 18:34
  • You can start off your array with a given size x, and whenever you need to, initialize another with size y, where y > x, and copy the contents of x and begin using the new allocated memory. Commented Apr 20, 2015 at 18:36

4 Answers 4

1

The ArrayList class extends AbstractList and implements the List interface.
ArrayList supports dynamic arrays that can grow as needed.

Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

It has three constructors:

ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)

Apart from that there are so many methods inherited from its parent class.
An example Program and Output:

import java.util.*;

public class ArrayListDemo {
   public static void main(String args[]) {
      // create an array list
      ArrayList al = new ArrayList();
      System.out.println("Initial size of al: " + al.size());

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());

      // display the array list
      System.out.println("Contents of al: " + al);
      // Remove elements from the array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: " + al.size());
      System.out.println("Contents of al: " + al);
   }
}


Output:

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]


Also read this: Java Dynamic arrays

Sign up to request clarification or add additional context in comments.

Comments

0

One thing you can do is ask user to input the value for number of Fibonacci numbers he/she wants, store it in a variable and give numbers = new int[n]; this way you can implement dynamic arrays in this way also.

Comments

0

Dynamic arrays are arrays which can grow in size such as ArrayList or vectors. ArrayList is used often.

here is a solution for your problem

import java.util.*;
public class fibo{
public static void main()
{
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(0);
a.add(1);
insertoneelement(a);
System.out.println(a.toString());
}
public static void insertoneelement(ArrayList<Integer> x)
{x.add((x.get(x.size()-2)+x.get(x.size()-1)));}
}

make your loop pass over insertoneelement(a) in main() as many times as many extra elements you want apart form initial 0 and 1.

Indexing of ArrayList is similar to that of arrays, index first element is 0 and of the last is length of array - 1. The size() method returns the number of elements in the ArrayList.

Comments

0

Here's an example of a manual dynamic array:

public static void main(String[] args) {
    // Fibnocci begins with 0, 1
    int[] fibNumbers = new int[] {0, 1};

    // Set your condition of how many numbers you want to create, 20 in my case.
    // This is also limited to the max value of an integer
    while (fibNumbers.length < 20) {
        // Create a new array for the next number in the sequence
        int[] newFibNumbers = new int[fibNumbers.length + 1];
        // Copy the cotents from your original array to this new array
        System.arraycopy(fibNumbers, 0, newFibNumbers, 0, fibNumbers.length);
        // Calculate the last element which is equal to the sum of the previous two elements
        newFibNumbers[newFibNumbers.length - 1] = newFibNumbers[newFibNumbers.length - 2] + newFibNumbers[newFibNumbers.length - 3];
        // Set the new array to the original array
        fibNumbers = newFibNumbers;
    }

    // Print your fibonacci series
    for (int i = 0; i < fibNumbers.length; i++) {
        System.out.println(fibNumbers[i]);
    }
}

Results:

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.