将一个数组元素与多个数组元素进行比较

时间:2014-04-22 18:25:15

标签: java arrays

我有两个文本文件包含如下数据: 文件A:

4,12-98-1,38.4611,APO,Tue,Jan,07,14:31:34,2014
5,12-111-1,0.9520,APO,Tue,Jan,07,11:56:21,2014,
6,12-176-1,8.0510,APO,Tue,Jan,07,11:58:10,2014,
7,12-214-1,165.2349,APO,Tue,Jan,07,14:33:19,2014
8,12-293-1,61.2558,APO,Tue,Jan,07,14:35:11,2014
9,12-805-1,2.0987,APO,Tue,Jan,07,12:03:30,2014,
10,12-986-1,3.7740,APO,Tue,Jan,07,12:05:16,2014,

档案B:

4,12-98-1,43.6546,APO,Tue,Jan,07,10:26:32,2014,
5,12-111-1,1.0430,APO,Tue,Jan,07,10:28:18,2014,
6,12-176-1,13.5606,APO,Tue,Jan,07,10:30:04,2014,
7,12-214-1,44.7091,APO,Tue,Jan,07,10:35:27,2014,
8,12-293-1,44.3001,APO,Tue,Jan,07,10:37:14,2014,
10,12-805-1,2.6451,APO,Tue,Jan,07,10:39:01,2014,

这些行中的每一行都存储在一个数组中。我想比较文件A的第一行的第一个数组元素与文件B中所有行的所有第一个数组元素,然后比较文件A的第二行的第一个数组元素以及所有的第一个数组元素文件B中的行并继续...直到文件的末尾。

我设法在阅读两个文本文件时获得匹配的内容。但是我不能匹配它,例如每个文件的最后一行,两个文件的第一个数组元素都是10。

我该怎么做?

这就是我所做的:

 while ( (TKN24_line2 = TKN24br2.readLine()) != null && (TKN2_line2 = TKN2br2.readLine()) != null){
        TKN24_array2 = TKN24_line2.split(",");
        int b = 0;
        for(String s : TKN24_array2){
            //System.out.println("values[" + b + "] = " + s);
            b++;
        }

        TKN2_array2 = TKN2_line2.split(",");
        int c = 0;
        for(String s : TKN2_array2){
            //System.out.println("values[" + c + "] = " + s);
            c++;
        }

        if(TKN24_array2[0].contentEquals(TKN2_array2[0])){
            //Print something.
        }

    }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

如果所有数据都不适合内存,则必须为文件A的每一行打开并读取文件B:

try (FileReader frA = new FileReader(filenameA);
    BufferedReader brA = new BufferedReader(frA)) {
    String lineA;
    while ((lineA = brA.readLine()) != null) {
        String value = lineA.split(",")[0];
        try (FileReader frB = new FileReader(filenameB);
            BufferedReader brB = new BufferedReader(frB)) {
            boolean found = false;
            String lineB;
            while ((lineB = brB.readLine()) != null) {
                String[] values = lineB.split(",");
                if (value.equals(values[0])) {
                    if (!found) {
                        System.out.println("Matches for " + value + ":");
                    }
                    System.out.println(lineB);
                    found = true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

如果数据确实适合内存,那么您可以将所有行读入两个列表,然后使用嵌套循环处理列表,或者您可以将文件B的行放入由Multimap索引的列表中。行开头的值,然后直接查找匹配的行。

ListMultimap<String, String> fileB = ArrayListMultimap.create();
try (FileReader frB = new FileReader(filenameB);
    BufferedReader brB = new BufferedReader(frB)) {
    String lineB;
    while ((lineB = brB.readLine()) != null) {
        String[] values = lineB.split(",");
        fileB.put(values[0], lineB); // map from first value to full line
    }
} catch (IOException e) {
    e.printStackTrace();
}

try (FileReader frA = new FileReader(filenameA);
    BufferedReader brA = new BufferedReader(frA)) {
    String lineA;
    while ((lineA = brA.readLine()) != null) {
        String value = lineA.split(",")[0];
        if (fileB.containsKey(value)) {
            System.out.println("Matches for " + value + ":");
            for (String line : fileB.get(value)) {
                System.out.println(line);
            }
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}