Java Create方法计算数组中char类型的出现次数
我需要创建一个方法来搜索字符串数组,无论数组中是否存在特定字符,并返回该字符出现次数的整数。我在其他文章中也曾尝试过自己解决这个问题,但它们都是int而不是String数组 我有另一个类名,因此数组名是type nameJava Create方法计算数组中char类型的出现次数,java,arrays,Java,Arrays,我需要创建一个方法来搜索字符串数组,无论数组中是否存在特定字符,并返回该字符出现次数的整数。我在其他文章中也曾尝试过自己解决这个问题,但它们都是int而不是String数组 我有另一个类名,因此数组名是type name public class Reg { //Fields private ArrayList<Name> Name; //Constructors public Reg() { Name = new ArrayL
public class Reg {
//Fields
private ArrayList<Name> Name;
//Constructors
public Reg() {
Name = new ArrayList<>();
}
//Methods
public int CountCharacterOccurrences (Char character){
}
}
公共类注册{
//田地
私有ArrayList名称;
//建设者
公共注册(){
Name=新的ArrayList();
}
//方法
公共整数countCharacterOccessions(字符){
}
}
名称类别:
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 + "]";
}
}
公共类名实现了可比较的{
//田地
私有字符串名;
私有字符串familyName;
//建设者
公共名称(){
firstName=“”;
familyName=“”;
}
公共名称(字符串firstName,字符串familyName){
this.firstName=firstName;
this.familyName=familyName;
}
//方法
public void setFirstName(字符串firstName){
this.firstName=firstName;
}
public void setFamilyName(字符串familyName){
this.familyName=familyName;
}
公共字符串getFirstName(){
返回名字;
}
公共字符串getFamilyName(){
返回家庭名称;
}
公共字符串getFullName(){
if(firstName.equals(“”)和familyName.equals(“”){
返回“”;
}否则{
返回firstName+“”+familyName;
}
}
@凌驾
公共字符串toString(){
return“Name:[firstName=“+firstName+”,familyName=“+familyName+”]”;
}
}
如何添加一个方法countCharacterOccurrences,该方法接受一个char参数并返回一个int来表示出现的次数。也许这就是您的意思:
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'));
}
}
class Reg{
私有数组列表名称;
公共注册(){
名称=新的ArrayList();
}
公共int countfirstnameOccurrencess(字符c){
返回name.stream()//获取流
.map(Name::getFirstName)//使用Name的firstName将流转换为流
.mapToInt(s->s.length()-s.replace(String.valueOf(c),“”).length()//计算每个firstName中出现的每个c并获得一个IntStream
.sum();//对所有值求和
}
公共静态void main(字符串[]args){
Reg Reg=新Reg();
注册名称。添加(新名称(“John”、“Doe”);
注册名称。添加(新名称(“约翰尼”、“Doe”);
注册名称。添加(新名称(“Richard”、“Roe”);
注册名称。添加(新名称(“詹姆斯”、“罗伊”);
注册名称。添加(新名称(“Jane”、“Roe”);
System.out.println(reg.countfirstnameaccurrencess('J');
}
}
输出:
四,
由于列表中的名字中有4个J(John、Johnny、James和Jane)对于初学者,您应该返回一个类型
int
,因为您需要计数,对吗?是否要在字符串数组中搜索字符?需要搜索的数组在哪里?我认为您还应该在需要搜索的函数中传递数组。方法签名应该类似于public int countfirstnameaccurrencess(char c,String[]strArray)
@clinomaniac数组在类中声明,我正在尝试添加一个方法,该方法接受一个char参数并返回一个int,表示特定字符的出现次数。这个类是什么!这正是我需要的谢谢你!如何编辑流,使其只计算每个字符串的名字和最后一个字母,因此对于您的示例,所有字母的输出都将是1现在这是一个不同的问题,我建议您在新帖子中提出。