从Csv文件中读取数据

时间:2016-07-12 06:48:44

标签: java csv

如何使用java程序从CSV文件中查找缺失编号的序列? 我正在使用Arraylist考虑一些数字。 我想读取整个CSV文件数据并找到数字上缺少的序列。我在文件中有大约1,00,000条记录。 程序: -

public class MissingNumber {

    public static long count = 0;
    public static int position = 0;
    public static boolean flag = false;

    public static void main(String[] args) {

        long a[] = {1054023,1054024,1054025,1054026,1054027,1054028,1054029,1054030,1054031,1054032,1054748,1054749,1054750,1054751,
            1054752,1054753,1054754,1054755,1054756,1054757,1054758,1055297,1055298,1055299,1055300,1055301,1055302,1055303,1055304,
            1055305,1055306,1055307,1055308,1055309,1056868,1057170,1057461,1057563,1057627,1057628,1057629,1057630,1057631,1057632,
            1057633,1057634,1057635,1057636,1057637,1057652,1057653,1057654,1057656,1057657,1057661,1057662,1057663,1057664,1057665,
            1057672,1057673,1057674,1057675,1057678,1057682,1057683,1057685,1057686,1057687,1057690,1057691,1057692,1057695,1057696,
            1057697,1057698,1057699,1057701,1057702,1057705,1057706,1057707,1057708,1057710,1057712,1057718,1057722,1057729,1057730,
            1057731,1057732,1057733,1057734,1057735,1057736,1057738,1057739,1057740,1057741,1057742,1057743,1057744,1057745,1057746,
            1057747,1057748,1057749,1057750,1057751,1057752,1057753,1057754,1057755,1057756,1057757,1057758,1057759,1057762,1057763,
            1057764,1057765,1057766,1057767,1057768,1057769,1057773,1057774,1057778,1057779,1057780,1057781,1057782,1057783,1057784,
            1057785,1057786,1057787,1057788,1057789,1057790,1057791,1057792,1057793,1057794,1057795,1057796,1057797,1057798,1057799,
            1057800,1057801,1057802,1057803,1057804,1057805,1057806,1057807,1057808,1057809,1057810,1057811,1057825,1057826,1057827,
            1057829,1057838,1057843,1057857,1057858,1057859,1057860,1057861,1057862,1057863,1057864,1057865,1057866,1057867,1057868,
            1057869,1057870,1057871,1057872,1057873,1057874,1057875,1057876,1057884,1057885,1057886,1057887,1057888,1057889,1057890,
            1057891,1057892,1057893,1057894,1057895,1057896,1057897,1057898,1057899,1057900,1057901,1057902,1057903,1057905,1057906,
            1057907,1057908,1057909,1057910,1057911,1057912,1057913,1057914,1057915,1057916,1057917,1057918,1057919,1057920,1057921};

        findMissingNumbers(a, position);

    }

    private static void findMissingNumbers(long a[], int position) {

        if (position == a.length - 1)
            return;

        for (; position < a[a.length - 1]; position++) {

            if ((a[position] - count) != position) 
            {
                System.out.println("position"+position);
                System.out.println("Missing Number: " + (position + count));
                flag = true;
                count++;
                break;
            }
        }

        if (flag) {
            flag = false;
            findMissingNumbers(a, position);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我假设您的数组已经排序。如果是这样的话,那么就像你手动完成那样找到缺失的nrs;比较两个邻居,如果差异超过一个打印数字丢失。像这样的东西:

public class MissingNumber {

    public static void main(String[] args) {
       long a[] = {1,2,3,5,9,11,23,24,25,26,27,28,39}; //try first on small arrays
       findMissingNumbers(a);

    }

    private static void findMissingNumbers(long a[]) {
        for(int i = 0; i<a.length-1; i++){
           if(a[i+1]- a[i] > 1){ 
               System.out.println("missing nr at index: " + (i+1));
               System.out.println("missing nrs");
               for (int j = 1;j<a[i+1]-a[i];j++){ // for ex. if a[i] = 13 and a[i+1]= 17 difference is 4 and there are 3 missing nrs.
                  System.out.println(a[i]+j);
               }
            }
        }
    }
}

为了轻松读取csv文件,我推荐opencsv