Java递归数组和最大和路径

时间:2014-03-10 15:39:22

标签: java arrays recursion path sum

好吧,我已经在这项任务上工作了一段时间,并且由于我一直遇到的砖墙而无法完全开始。 基本上我需要做的是从文件中获取数据并递归地用数据填充不同的2d int数组 例如.txt文件可能包含:

2 <-- number of 2d arrays to have
3 <-- L X W of the 2d array
1 2 4 <-|
4 7 9 <-|-- Data to fill 2d array
2 1 6 <-|
2 <-- L X W of the 2d array
3 4   <-|-- Data to fill 2d array
5 9   <-|

所以我在想的是我会使用大小来添加到索引中,以便跳过已经放入数组的数据块。所以一个阵列会有     1 2 4     4 7 9     2 1 6 在它里面,我可以和其他人一起工作     3 4     5 9 香港专业教育学院尝试了这么多的方法,但它必须是递归的,并能够处理这种格式的任何数据..任何想法?

1 个答案:

答案 0 :(得分:0)

    String[size][size] myArray = new String[size][size];
    int row = 0 ;
    int column = 0;

    BufferedReader br = null;

    try {

        String line;

        br = new BufferedReader(new FileReader("The File Path HERE!"));

        //for each line in file
        while ((line= br.readLine()) != null) {

            //split on white space
            String[] row = line.split("\\s+");

            //column index
            for(int i =0; i < row.length; i++){
                  String context = row[i];
                  myArray[row][column] = context;
                  row++;
                  column++;
                  if(row == size) row = 0;
                  if(column == size) column = 0;                
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }