【问题标题】:Create method to count occurrences of type char within an array创建方法来计算数组中 char 类型的出现次数
【发布时间】:2018-01-09 18:22:45
【问题描述】:

我需要创建一个方法来搜索字符串数组中是否存在特定字符并返回该字符出现次数的整数,我查看了其他帖子以尝试自己解决,但他们都是 int 的数组不是 String

我有另一个类名,因此数组名是 Name 类型。

public class Reg {

    //Fields
    private ArrayList<Name> Name; 


    //Constructors
    public Reg() {
        Name = new ArrayList<>();
    }

    //Methods

    public int CountCharacterOccurrences (Char character){

    }
}

名称类:

public class Name implements Comparable<Name> {

    //Fields
    private String firstName;
    private String familyName;


    //Constructors
    public Name() {
        firstName = "";
        familyName = "";
    }

    public Name(String firstName, String familyName) {
        this.firstName = firstName;
        this.familyName = familyName;
    }


    //Methods
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getFamilyName() {
        return familyName;
    }

    public String getFullName() {
        if (firstName.equals("") && familyName.equals("")) {
            return "";
        } else {
            return firstName + " " + familyName;
        }
    }

    @Override
    public String toString() {
        return "Name:[firstName=" + firstName + ", familyName=" + familyName + "]";
    }
}

如何添加一个方法 CountCharacterOccurences,它接受一个 char 参数并返回一个表示出现次数的 int。

【问题讨论】:

  • 对于初学者,你应该返回一个类型 int 因为你想要 count ,对吧?
  • 你想在字符串数组中搜索一个字符吗?
  • 您需要搜索的数组在哪里?我相信您还应该在需要搜索的函数中传递数组。方法签名应该类似于public int countFirstNameOccurrences(char c, String[] strArray)
  • @clinomaniac 数组在类中声明,我正在尝试添加一个接受 char 参数并返回表示特定字符​​出现次数的 int 的方法。
  • 这个类是什么Name???

标签: java arrays


【解决方案1】:

也许这就是你的意思:

class Reg{
    private ArrayList<Name> names;

    public Reg() {
        names = new ArrayList<>();
    }

    public int countFirstNameOccurrences(char c) {
        return names.stream()            // get Stream<Name>
                .map(Name::getFirstName) // convert Stream<Name> to Stream<String> using Name's firstName
                .mapToInt(s -> s.length() - s.replace(String.valueOf(c), "").length()) // calculate every occurrence c in each firstName and get an IntStream
                .sum();                  // sum all the values
    }

    public static void main(String[] args) {
        Reg reg = new Reg();
        reg.names.add(new Name("John", "Doe"));
        reg.names.add(new Name("Johnny ", "Doe"));
        reg.names.add(new Name("Richard", "Roe"));
        reg.names.add(new Name("James", "Roe"));
        reg.names.add(new Name("Jane", "Roe"));

        System.out.println(reg.countFirstNameOccurrences('J'));
    }
}

输出:

4

因为列表中的名字中有 4 个 J(John、Johnny、James 和 Jane)

【讨论】:

  • 太棒了!这正是我需要的谢谢!
  • 如何编辑流,使其仅计算每个字符串的名字最后一个字母,因此对于您的示例,所有字母的输出将为 1
  • 现在这是一个不同的问题,我建议您在新帖子中提出。
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 2016-03-29
  • 2017-11-03
  • 2021-11-06
  • 2020-08-29
  • 2019-02-26
  • 2015-12-07
  • 2015-06-18
相关资源
最近更新 更多