Java 动态数组斐波那契

Java 动态数组斐波那契,java,fibonacci,Java,Fibonacci,我需要转换一个代码,打印出同样使用动态数组的斐波那契数列,这是我到目前为止得到的,我只是不知道如何将它转换成动态数组 public class Fibonacci { public static void main(String[] args) { int[] numbers; numbers = new int[20]; numbers[0] = 1; numbers[1] = 1; System.out.

我需要转换一个代码,打印出同样使用动态数组的斐波那契数列,这是我到目前为止得到的,我只是不知道如何将它转换成动态数组

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]);
        }
    }
}
公共类Fibonacci{
公共静态void main(字符串[]args){
int[]数字;
数字=新整数[20];
数字[0]=1;
数字[1]=1;
System.out.println(“\nFBONACCI系列:\n”);
System.out.println(数字[0]);
System.out.println(数字[1]);
对于(int i=2;i<20;i++){
数字[i]=数字[i-2]+数字[i-1];
System.out.println(数字[i]);
}
}
}

您可以做的一件事是让用户输入他/她想要的斐波那契数的值,将其存储在变量中并给出 数字=新整数[n];
通过这种方式,您也可以通过这种方式实现动态数组。

ArrayList类扩展了AbstractList并实现了List接口。
ArrayList支持可以根据需要增长的动态数组。

标准Java数组的长度是固定的。创建阵列后,它们不能增长或收缩,这意味着您必须提前知道阵列将容纳多少个元素

数组列表是以初始大小创建的。超过此大小时,集合将自动放大。移除对象后,阵列可能会缩小。

它有三个构造函数:

ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)
除此之外,还有许多方法继承自其父类。
示例程序和输出:

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);
   }
}

输出:


也请阅读以下内容:

动态数组是可以增大大小的数组,如ArrayList或vectors。ArrayList经常使用

这是解决你问题的办法

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)));}
}
import java.util.*;
公共级fibo{
公共静态void main()
{
ArrayList a=新的ArrayList();
a、 加(0);
a、 增加(1);
插入元件(a);
System.out.println(a.toString());
}
公共静态void insertoneelement(ArrayList x)
{x.add((x.get(x.size()-2)+x.get(x.size()-1));}
}
将循环传递到main()中的insertoneelement(a)上的次数是您希望从初始值0和1中分离的额外元素数的两倍


ArrayList的索引类似于数组的索引,索引的第一个元素是0,最后一个元素是数组的长度-1。size()方法返回ArrayList中的元素数。

以下是手动动态数组的示例:

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]);
    }
}
publicstaticvoidmain(字符串[]args){
//Fibnocci以0,1开头
int[]fibNumbers=新的int[]{0,1};
//设置要创建多少个数字的条件,我的情况是20。
//这也仅限于整数的最大值
而(fibNumbers.length<20){
//为序列中的下一个数字创建新数组
int[]newFibNumbers=新int[fibNumbers.length+1];
//将元素从原始数组复制到此新数组
数组复制(fibNumbers,0,newFibNumbers,0,fibNumbers.length);
//计算最后一个元素,该元素等于前两个元素之和
newFibNumbers[newFibNumbers.length-1]=newFibNumbers[newFibNumbers.length-2]+newFibNumbers[newFibNumbers.length-3];
//将新数组设置为原始数组
fibNumbers=新的fibNumbers;
}
//打印斐波那契数列
对于(int i=0;i
结果:


您真的需要动态调整数组的大小,还是只允许使用
ArrayList
s/int[]/List/g
。完成!您可以使用给定的大小x启动阵列,并在需要时使用大小y初始化另一个阵列,其中y>x,然后复制x的内容并开始使用新分配的内存。
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]);
    }
}