计数方法 Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33748415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:00:28  来源:igfitidea点击:

Count Method Java

javamethodscountnames

提问by

I have an input file called input.txt with a list of names. I have no problem displaying all the names and putting them in alphabetical order with both display and sort methods. But what I am currently struggling to do is create a method where I can count the recurrence of each name in the file. I would grealty appreciate if anyone could help me with this, and find a way to create this method.

我有一个名为 input.txt 的输入文件,其中包含一个名称列表。我可以使用 display 和 sort 方法显示所有名称并将它们按字母顺序排列。但我目前正在努力做的是创建一种方法,我可以计算文件中每个名称的重复次数。如果有人可以帮助我并找到创建此方法的方法,我将不胜感激。

public class Names {

public static void display(ArrayList<String> names) {
    for (int i = 0; i < names.size(); i = i + 1) {
        System.out.println(names.get(i));
    }
}

public static int find(String s, ArrayList<String> a) {
    for (int i = 0; i < a.size(); i = i + 1) {
        String str = a.get(i);
        if (str.equals(s)) {
            return i;
        }
    }
    return -1;

}

public static void capitalize(ArrayList<String> names) {
    for (int i = 0; i < names.size(); i = i + 1) {
        String name = names.get(i);
        if (!name.isEmpty()) {
            String firstLetter = "" + name.charAt(0);
            names.set(i, firstLetter.toUpperCase() + name.substring(1).toLowerCase());

        }
    }
}

public static void sort(ArrayList<String> names) {
    for (int i = 0; i < names.size() - 1; i = i + 1) {
        int Min = i;
        for (int j = i + 1; j < names.size(); j = j + 1) {
            if (names.get(j).compareTo(names.get(Min)) < 0) {
                Min = j;
            }
        }
        String tmp = names.get(i);
        names.set(i, names.get(Min));
        names.set(Min, tmp);

    }

}

public static void getNames(ArrayList<String> fn, ArrayList<String> ln) throws IOException {
    Scanner kb = new Scanner(System.in);
    System.out.println("What is the input flie?");
    String names = kb.next();
    File inpFile = new File(names);
    Scanner in = new Scanner(inpFile);

    while (in.hasNext()) {
        String firstName = in.next();
        String lastName = in.next();
        fn.add(firstName);
        ln.add(lastName);

    }

}

private int countOccurence(String name, ArrayList<String> names){
 int count = 0;
 for(int i =0; i <= names.size; i++){
    if(name.equalsIgnoreCase(names.get(i))){
        count++;
    }
 }
 return count;

}

}

public static void main(String[] args) throws IOException {

    ArrayList<String> first = new ArrayList<>();
    ArrayList<String> last = new ArrayList<>();
    getNames(first, last);
    capitalize(first);
    capitalize(last);

    ArrayList<String> allNames = new ArrayList<>();
    for (int i = 0; i < first.size(); i++) {
        allNames.add(last.get(i) + ", " + first.get(i));
    }
    System.out.println("*******All Names******");

    sort(allNames);
    display(allNames);

    System.out.println("*****First Name Count***");

    for(int i =0; i <= first.size; i++){
    int count = countOccurence(first.get(i), first);
System.out.println(first.get(i) + " occured " + count + " times.");

}

}

    System.out.println("****Last Name Count****");

    sort(last);
    display(last);

}

}

}

回答by codeaholicguy

Use Map structure for those case:

在这些情况下使用 Map 结构:

Map<String, Integer> recurence = new HashMap<>();
int count;
for (String name : names) {
    if (recurence.containsKey(name)) {
        count = recurence.get(name) + 1;
    } else {
        count = 1;
    }
    recurence.put(name, count);
}

回答by jmcg

create a method that counts the occurences:

创建一个计算出现次数的方法:

public static int countOccurence(String name, ArrayList<String> names){
     int count = 0;
     for(int i =0; i <= names.size(); i++){
        if(name.equalsIgnoreCase(names.get(i))){
            count++;
        }
     }
     return count;
}

To use it, go through the loop in you Main( or you can create another method)

要使用它,请遍历您的循环Main(或者您可以创建另一种方法)

for(int i =0; i <= first.size; i++){
    int count = countOccurence(first.get(i), first);
    System.out.println(first.get(i) + " occured " + count + " times.");
}