使用动态编程和递归的Java中的斐波那契

问题描述 投票:1回答:1

我是动态编程的新手,所以我尝试创建一个文件并将其写入下载部分的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[] arrayO) {
        try {
            FileWriter myWriter = new FileWriter("C:\\Users\\YOURUSERNAME\\Downloads\\Array.txt");
            if (arrayO.length <= array.length) {
                System.arraycopy(arrayO, 0, array, 0, arrayO.length);
                myWriter.write(Arrays.toString(array));
                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, array);
            }
        }
        return array[num - 1];
    }
    public static void main(String[] args) {
        int num = 10;


        int[] array = arrayCreator(num);
        createFile();
        fileWrite(array, array);
        System.out.println(fib(num, array));
    }
}
java arrays recursion dynamic-programming filewriter
1个回答
0
投票

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

  • 使用System.arraycopy()

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

    Arrays.copyOf()
© www.soinside.com 2019 - 2024. All rights reserved.