java - Java 计数方法

标签 java methods count names

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

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);

}

}

最佳答案

在这些情况下使用映射结构:

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);
}

关于java - Java 计数方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748415/

相关文章:

java - 为什么我在使用 SAXParser 时得到 "MalformedURLException: no protocol"?

java - 从对象集合中测试集合属性的内容

python - 如何正确运行类中方法内的方法(python)

java - 何时在泛型方法声明中使用 <T>

jquery - 使用 jQuery 计算数据属性的值

sql - SQL查询列信息计数

java - 如何在spring mvc项目中导入StreamingResponseBody?

java - 为什么我的内容 uri 出现错误?

java - 为什么不抽象字段?

r - 计算 R 中的唯一分类值