如何在数组中添加字符数? [英] How to add character amounts in an array?

查看:118
本文介绍了如何在数组中添加字符数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字符串数组,需要取每个字符串中的字符数量,并将它们加在一起以获得1个总数。我将如何去做呢?

So I have an array of strings and need to take the amount of characters in each string and add them together to get 1 total. How would I go about doing this?

这里是数组:

public class Final21 {
   public static String getLongString(String[] array) {

      String[] names = {"bob", "maxwell", "charley", "tomtomjack"};

   }
}

我没有添加索引,但是每个字符串中的字符数。
示例:bob有3个字符,tomtomjack有10个字符,如果添加它们,则为13

I am not adding indexes, but the amount of characters in each string. Example: bob has 3 characters, tomtomjack has 10 characters, if you add them it would be 13

尝试:

       public static int countAllLetters(String[] array) {

      String[] names = {"bob", "maxwell", "charley", "tomtomjack"};                

         int sum = 0;
         for(String s : array)
            sum += s.length();
               return sum;

      int amountOfLetters = countAllLetters(names);
         System.out.println(amountOfLetters);

   }

给出错误:

Final21.java:62: error: unreachable statement
      int amountOfLetters = countAllLetters(names);
          ^
Final21.java:65: error: missing return statement
   }
   ^
2 errors


推荐答案

有关Java 8+解决方案,请参考Aominè的答案

For a Java 8+ solution, refer to this answer by Aominè.

在注释中,您说您不能使用Java 8。 Java 8环境。

In the comments, you say you can't use Java 8. This answer exposes a solution for pre-Java 8 environments.

如果要返回 int 包含数组中每个 String 中字符的总和,您需要更改方法的返回类型。

If you want to return an int containing the combined amount of characters from every String in the array, you need to change the return type of your method.

public static int countAllLetters(String[] array)

注意如何更改名称以更好地表达此方法的行为。

Notice how I changed the name to better express the behavior of this method.

要实现该方法,只需遍历 array 并将每个 String length()加在一起:

To implement it, simply loop through the array and add together the length() of each String:

public static int countAllLetters(String[] array) {
    int sum = 0;
    for(String s : array)
        sum += s.length();
    return sum;
}

这将用作:

public static void main(String[] args) {
    String[] names = { "bob", "maxwell", "charley", "tomtomjack" };
    int amountOfLetters = countAllLetters(names);

    System.out.println(amountOfLetters);
}

因此,您的最终结果将是:

So your finished result would be:

public class YourClass {
    public static void main(String[] args) {
        String[] names = { "bob", "maxwell", "charley", "tomtomjack" };
        int amountOfLetters = countAllLetters(names);

        System.out.println(amountOfLetters);
    }

    public static int countAllLetters(String[] array) {
        int sum = 0;
        for(String s : array)
            sum += s.length();
        return sum;
    }
}

点击此处使用在线编译器进行测试

还要注意我怎么做不要在方法内部声明 names 数组。相反,我在数组外部声明了它,然后将其作为参数传递给了方法。这使得该方法可用于不同的数组,而不是单个硬编码的 names 数组。

Also notice how I don't declare the names array inside the method. Instead, I declared it outside of the array, then passed it into the method as an argument. This allows the method to be reusable for different arrays, rather than a single hard-coded names array.

但是,如果要返回数组的 String 内容组合(基于您在问题中显示的名称和返回类型),您需要将方法的返回类型保持为 String ,并合并项目来自数组:

However, if you want to return a String of array's content combined (based on the name & return type you're showing in your question), you need to keep the return type of your method as String, and concat the items from the array:

public static String concat(String[] array) {
    StringBuilder builder = new StringBuilder();
    for(String s : array)
        builder.append(s);
    return builder.toString();
}

这篇关于如何在数组中添加字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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