Java 中的斐波那契使用动态规划和递归

Fibonacci in Java using Dynamic Programming and Recursions

我是动态规划的新手,所以我尝试创建一个文件并将其写入下载部分的 Array.txt。逻辑是:

If the Array is longer than the current array: Extend the array and copy to old array onto the new one

但是,我找不到将旧数组的部分实际复制到新数组的方法。 我现在使用的方式是

System.arraycopy()

代码如下: (请记住将 YOURUSERNAME 换成您当前在计算机上的用户名,以免出错)

import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.util.Arrays;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Fibonacci {

    static void createFile() {
        try {
            File Array = new File("C:\Users\YOURUSERNAME\Downloads\Array.txt");
            if (Array.createNewFile()) {
                System.out.println("File created: " + Array.getName());
            } else {
                System.out.println("File already exists.");
            }
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }

    static void fileWrite(int[] array, int num) {
        try {
            FileWriter myWriter = new FileWriter("C:\Users\YOURUSERNAME\Downloads\Array.txt");
            if (num <= array.length) {
                int[] newA = new int[array.length];
                System.arraycopy(array, 0, newA, 0, array.length);
                myWriter.write(Arrays.toString(newA));
                myWriter.close();
            }
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }


    static int[] fileRead(int[] Array1) {
        try {
            StringBuilder dataTotal = new StringBuilder();
            File Array = new File("C:\Users\YOURUSERNAME\Downloads\Array.txt");
            Scanner myReader = new Scanner(Array);
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                dataTotal.append(data);
            }
            myReader.close();
            System.out.println(dataTotal);
            dataTotal.deleteCharAt(0);
            dataTotal.deleteCharAt(dataTotal.length() - 1);
            String[] array1 = dataTotal.toString().split(", ");
            int[] array = new int[array1.length];
            for (int i = 0; i < array1.length; i++) {
                array[i] = Integer.parseInt(array1[i]);
            }
            return array;
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
            return null;
        }
    }

    static int[] arrayCreator(int num) {
        return new int[num];
    }

    static int fib(int num, int[] array1) {
        int[] array = fileRead(array1);
        if (num == 1 || num == 2) {
            return 1;
        }
        else {
            assert array != null;
            if(array[num - 1] > 0) {
                return array[num - 1];
            } else {
                array[num - 1] = fib(num - 1, array1) + fib (num - 2, array1);
                fileWrite(array, num);
            }
        }
        return array[num - 1];
    }
    public static void main(String[] args) {
        int num = 10;


        int[] array = arrayCreator(num);
        createFile();
        fileWrite(array, num);
        System.out.println(fib(num, array));
    }
}

要将数组扩展一个元素,您有两种选择:

  • 使用System.arraycopy()

    int[] newArray = new int[array.length + 1];
    System.arraycopy(array, 0, newArray, 0, array.length);
    array = newArray;
    
  • 使用Arrays.copyOf()

    array = Arrays.copyOf(array, array.length + 1);