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

标签 java arrays file

好的,所以我的程序获取一个文件(该文件是 401 x 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: ");

    }

}

哦,如果您尝试运行代码以了解它是如何工作的并且需要我使用的文件,请给我发电子邮件,我会将其发送给您,我真的很感谢任何帮助:) My E -邮件是Spooce199@hotmail.co.uk 希望每个人都度过了一个愉快的圣诞节:)

最佳答案

如果您以某种方式附加了该文件,那就太好了...但是,从它的外观来看,我认为这是这样的

    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 放入粘贴箱并包含链接。

关于java - 在文件数组中查找特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032982/

相关文章:

java - 无法为JSP编译类,只能导入一个类型

Java 迭代器双向链表

Python:如何打印列表,其中包含列表中每个项目的标签

c++ - 如何在 C++ 中使用 asio.boost 修改 Web 上文件的内容?

c - stat.h 在 Linux 中不可用?

java - 试图找到一种方法将解析 Int 合并到我的计算器中

java - 拖放 JTableHeader

Java FloodFill Problem 堆栈溢出错误

c# - 将动态长度的数组插入和更新到表中

java - 放置双值二维数组读取文本