在文件数组中查找特定数据?

时间:2012-12-25 18:21:19

标签: java arrays file

好吧,所以我的程序获取一个文件(文件是由401个数字隔开一个空格的401)并将它们写入一个数组,然后它将数据拆分并打印出来。执行此操作后,我想告诉程序从该文件中找到特定的数字(主要是最高数字和最低数字)。我已经尝试了mathmax和mathmix之类的方法,所以如果数组被称为“dataArray”,我试图说“Math.max(dataArray,0)”但是htis失败了。我在搜索这个网站后尝试了其他解决方案,但没有任何效果。有人可以帮我找到这个阵列中的数据吗?非常感谢,这是迄今为止的代码。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;  //import tools

public class MultiArray {
    public static void main(String[] args)throws IOException {

        //variables
        int rows = 401;
        int columns = 401;
        String file = "dmt.asc";

        double dmtData[][] = new double[rows][columns];  //array

        BufferedReader Reader = new BufferedReader(new FileReader(file)); //read the file

        //split the numbers and write to array
        for(int i = 0; i < rows; i++){
            String rowArray [] = Reader.readLine().split(" ");
            for(int j = 0; j < columns; j++){
                dmtData[i][j] = Double.parseDouble(rowArray[j]);
            }
        }

        Reader.close();  //close the reader and the file

        //print out the array
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < columns; j++){
                System.out.println(dmtData[i][j]);
            }
        }

        //code here to find highest number
        System.out.println("The highest peak in this area is: ");

        //code here to find lowest number
        System.out.println("The lowest dip in this area is: ");

    }

}

哦,如果您正在尝试运行代码以了解它是如何工作的并且需要我使用的文件,请发送电子邮件给我,我会发送给您,我非常感谢任何有用的帮助:)我的E -Mail是Spooce199@hotmail.co.uk希望每个人都有一个可爱的圣诞节:)

1 个答案:

答案 0 :(得分:1)

如果你以某种方式附加文件会很棒......但是,从它的外观来看,我认为这个

    double high = Double.MIN_VALUE;
    double low = Double.MAX_VALUE; 

    for(int i = 0; i < dmtData.length; i++){
        for(int j = 0; j < dmtData[i].length; j++){
            if(dmtData[i][j] > high){
                high = dmtData[i][j];
            }
            else if(dmtData[i][j] < low){
                low = dmtData[i][j];
            }
        }
    }

应从dmtData获取最大值和低值。我没有测试它,所以尝试它或粘贴垃圾箱中的dmt.asc并包含链接。