【问题标题】:Fibonacci in Java using Dynamic Programming and RecursionsJava中使用动态编程和递归的斐波那契
【发布时间】:2020-07-24 07:12:32
【问题描述】:

我是动态编程的新手,所以我尝试创建一个文件并将其写入下载部分的 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));
    }
}

【问题讨论】:

  • fileWrite() 方法应该只负责将数组写入文件,它不应该进行任何数组复制。您的代码应该在内存中构建数组,最后只调用fileWrite() once。退后一步,重新思考你在做什么。
  • 我认为使用 fileWrite() 复制数组然后将其写入文本文件是最合乎逻辑的做法,因为它可以节省大量编译时间,因为我不需要添加新功能只需使用一次。
  • 写入文件需要时间,因此通过将整个内容重复写入文件,而不是等到最后,肯定不会“节省大量时间”,它会不必要地减慢程序的速度(很多)。 --- 此外,“关注点分离”的编程指南意味着名为writeFile 的方法应该这样做,将 an 数组的内容写入文件。它不应该负责将值从一个数组复制到另一个数组。
  • 好的,我应该再做一个函数?
  • 我不知道。你为什么要做另一个功能。您需要数组比现在更大的地方在哪里?为什么不在代码中的那个点扩展数组长度?

标签: java arrays recursion dynamic-programming filewriter


【解决方案1】:

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

  • 使用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);
    

【讨论】:

  • 我不想将数组扩展一个元素,我希望它将现有的结果复制到新数组中,而新数组不仅限于 1 个元素
  • @MaxPan 这是一个示例。指定您希望新数组具有的任何新大小。
  • 好的,但这不是我在代码中所做的吗? System.arraycopy(arrayO, 0, array, 0, arrayO.length);
  • @MaxPan 你在哪里做的?我能看到的唯一arraycopy() 是在fileWrite() 方法中,它当然不属于该方法,并且您始终使用与两个参数相同的数组来调用它,那么您在哪里做 " 扩展数组并按照指示将旧数组复制到新数组上”
  • 好吧,我不是那样做的,所以参数是“int num, int[] array”,其中 num 是元素的数量,array 是要更新的数组。您可以再次在帖子中看到新的 fileWrite()。
猜你喜欢
  • 1970-01-01
  • 2010-12-03
  • 2011-08-02
  • 2019-01-21
  • 2012-02-16
  • 1970-01-01
  • 2020-07-15
  • 2012-11-06
相关资源
最近更新 更多