Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将二维字符串数组解析为二维整数数组_Java_Arrays_Sorting - Fatal编程技术网

Java 将二维字符串数组解析为二维整数数组

Java 将二维字符串数组解析为二维整数数组,java,arrays,sorting,Java,Arrays,Sorting,我正在寻找一种方法来关联此文件中的每一行(csv),并获取与商店名称关联的每月最大值(每列表示月份)。我可以得到行最大值和列最大值,但我不知道如何将每个列最大值链接到它们的城市名称。救命啊 文件如下: Omaha,104,1218,418,216,438,618,274,234,510,538,740,540 Saint Louis,72,1006,392,686,626,670,204,286,236,344,394,930 Des Moines,116,1226,476,330,444,46

我正在寻找一种方法来关联此文件中的每一行(csv),并获取与商店名称关联的每月最大值(每列表示月份)。我可以得到行最大值和列最大值,但我不知道如何将每个列最大值链接到它们的城市名称。救命啊

文件如下:

Omaha,104,1218,418,216,438,618,274,234,510,538,740,540
Saint Louis,72,1006,392,686,626,670,204,286,236,344,394,930
Des Moines,116,1226,476,330,444,464,366,230,602,260,518,692
Chicago,408,948,80,472,626,290,372,282,488,456,376,580
Kansas City,308,1210,450,234,616,414,500,330,486,214,638,586
Austin,500,812,226,470,388,488,512,254,210,388,738,686
Houston,454,1086,430,616,356,534,218,420,494,382,476,846
New Orleans,304,1278,352,598,288,228,532,418,314,496,616,882
以下是我目前的代码:

File selectedFile = Utilities.selectFile("Type the file name: Ex:annualUnitsSold.csv        \n");
int numberRecords = Utilities.countLinesInFile(selectedFile);
String[][] records = Utilities.loadStringsFromFile(selectedFile, numberRecords);

System.out.println(columnCompare(records,1);
public static int columnCompare (String[][] array, int col)
{
    String report = "";
    int monthMax = 0;

    for(int row = 0; row < array.length; row++)
    {
        if(monthMax < Integer.parseInt(array[row][col]))
        {   
            monthMax = Integer.parseInt(array[row][col]);
        }
    }
    return monthMax;
}
//NOTE: when i run this it works as long as i put the column number greater than zero.
File selectedFile=Utilities.selectFile(“键入文件名:Ex:annualUnitsSold.csv\n”);
int numberRecords=Utilities.countLinesInFile(selectedFile);
String[][]记录=实用程序。加载stringsfromfile(selectedFile,numberRecords);
System.out.println(columnCompare(记录,1);
公共静态整型列比较(字符串[][]数组,整型列)
{
字符串报告=”;
int-monthMax=0;
for(int row=0;row
使用HashMap

HashMap<String, Integer> cities = new HashMap<>();
...
cities.put(city, columnCompare(...));
HashMap cities=newhashmap();
...
城市。放置(城市,柱比较(…);

您可以查找索引,而不是查找值:

int month = 1;
int store = columnCompare(records, month);
System.out.println(records[store, 0] + ": " + records[store, month]);
public static int columnCompare (String[][] array, int col)
{
    int rowMax = 0;
    int monthMax = -1;
    for(int row = 0; row < array.length; row++)
    {
        int val = Integer.parseInt(array[row][col]);
        if(monthMax < val)
        {
            rowMax = row;
            monthMax = val;
        }
    }
    return rowMax;
}
int月=1;
int store=列比较(记录,月份);
System.out.println(记录[store,0]+“:“+记录[store,month]);
公共静态整型列比较(字符串[][]数组,整型列)
{
int rowMax=0;
int monthMax=-1;
for(int row=0;row
你的问题是什么?我需要找到一种关联第一列(城市)的方法,以便它们能够表示每个列中的每月最大值。例如:第1列:500;但现在如何获得它,以便它显示奥斯汀?城市名称在第0列中,因此当你找到最大值时,还记得数组中关联的城市[row][0]。非常感谢。我不知道为什么我会这么困惑。我无法使用哈希映射来完成此作业。谢谢,尽管您没有提到约束。约束是什么?