如何从一个文件中的顶部4个整数。 [英] How to get the top 4 integers from a file.

查看:145
本文介绍了如何从一个文件中的顶部4个整数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中的姓名,性别的列表和多少人有这个名字的计数。

例如:

阁楼,男,416

莎拉,女,800

伯纳德,男,413

杰西卡,女,1300

巴里,男,408

德里克,男,407

米切尔,男,407

拿,男,404

我想获得前4名的有名字的最高数,但我无法这样做。我曾经尝试过,但它似乎并没有工作。我越来越找不到符号错误。此外,我想知道是否有这样的功能,更有效的方式,因为如果我必须找到一个更大的文件的前10名,那我做的可以得到很长的路。

 而(sc.hasNextLine())
    {    //通过SC成线从输入读取文件中的行
        行= sc.nextLine();        StringTokenizer的STK =新的StringTokenizer(行,,);
        字符串名称= stk.nextToken();
        炭性别= stk.nextToken()的charAt(0)。
        诠释计数=的Integer.parseInt(stk.nextToken());
        OneName名单=新OneName(姓名,性别,计数);        oneName.add(名单);
    }    字符串许多= oneName.get(0)的ToString();
    INT A = oneName.get(0).getCount();
    的for(int i = 0; I< oneName.size();我++)
    {        INT B = oneName.get(ⅰ).getCount();
        INT C = oneName.get(I).getCount();
        INT D = oneName.get(I).getCount();
        字符串男子= oneName.get(I)的ToString();
        如果(A< B)
        {
            A = B;
            B = A;        }
        如果(B< c)
        {
            B = C;
            C = B:
        }
        如果(C< D​​)
        {
            C = D组;
            D = C:        }    }    的System.out.println(A + B + C + D);
    }


解决方案

我不知道 oneName 的类型。如果它是一个列表,你可以使用 Col​​lections.sort()方法从标准的API:

  Col​​lections.sort(oneName,新的比较< OneName>()
    {
        公众诠释比较(OneName O1,O2 OneName)
        {
            返回o2.getCount() - o1.getCount();
        }
    });

然后 oneName 首4个元素将是具有最高计数的人。

在您的code,只是上面的调用 Col​​lections.sort取代你的最后一个循环()。然后,您可以得到4最高计数(你必须检查的范围数组索引):

  OneName A = oneName.get(0);
OneName B = oneName.get(1);
OneName C = oneName.get(2);
OneName D = oneName.get(3);

I have a file where the is a list of names, gender and a count of how many people have that name.

Example:

Garret,M,416

Sarah,F,800

Bernard,M,413

Jessica,F,1300

Barry,M,408

Derick,M,407

Mitchel,M,407

Nathanael,M,404

I am trying to get the top 4 names that have the highest count of names, but I am having trouble doing so. I have tried it but it doesn't seem to work. I am getting can't find symbol errors. Also i wanted to know if there was a more efficient way of doing this function because if I have to find the top 10 of an even bigger file, the way that I'm doing it could get really long.

    while(sc.hasNextLine())
    {

    // read a line from the input file via sc into line
        line = sc.nextLine();



        StringTokenizer stk = new StringTokenizer(line, ",");
        String name = stk.nextToken();
        char sex = stk.nextToken().charAt(0);
        int count = Integer.parseInt(stk.nextToken());


        OneName list = new OneName(name, sex, count);

        oneName.add(list);      


    }

    String many= oneName.get(0).toString();
    int a = oneName.get(0).getCount();
    for (int i = 0; i< oneName.size(); i++)
    {

        int b = oneName.get(i).getCount();
        int c = oneName.get(i).getCount();
        int d = oneName.get(i).getCount();
        String man= oneName.get(i).toString();
        if ( a < b)
        {
            a = b;
            b = a ;

        }
        if (b < c)
        {
            b = c; 
            c = b; 
        }
        if (c < d)
        {
            c = d;
            d = c;

        }

    }

    System.out.println(a+b+c+d);
    }

解决方案

I don't know the type of oneName. If it's a list, you can use the Collections.sort() method from standard API:

Collections.sort(oneName, new Comparator<OneName>()
    {
        public int compare(OneName o1, OneName o2)
        {
            return o2.getCount() - o1.getCount();
        }
    });

Then the first 4 elements of oneName will be the ones with the highest count.

In your code, just replace your last loop by the above call to Collections.sort(). You can then get the 4 highest counts (you'll have to check for array index in bounds):

OneName a = oneName.get(0);
OneName b = oneName.get(1);
OneName c = oneName.get(2);
OneName d = oneName.get(3);

这篇关于如何从一个文件中的顶部4个整数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆