Java 计算字符串数组中的字母数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19942716/
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-12 21:44:06  来源:igfitidea点击:

Calculate number of letters in string array?

javaarraysstringchar

提问by

I am trying to calculate the total number of letters within the array? I was attempting to change the array to string, then calculate total string length (code below) but cant get it to work? (point of interest in Bold) Thanks to anyone in advance.

我想计算数组中的字母总数?我试图将数组更改为字符串,然后计算总字符串长度(下面的代码)但无法让它工作?(粗体的兴趣点)提前感谢任何人。

package practical5;

import java.util.Arrays;

public class Part1_9 {

public static void main(String[] args) {

    // declaring and populating array
    String quoteArray[] = { "\"Continuous", "effort", "not", "strength",
            "nor", "intelligence", "is", "the", "key", "to", "unlocking",
            "our", "potential.\"\n" };

    // for loop to print full array
    for (int counter = 0; counter < quoteArray.length; counter++) {
        System.out.print(quoteArray[counter] + " ");
    }// end of for loop

    // Printing array using Enhanced for/ for each loop (Different way to
    // print array)
    for (String element : quoteArray) {
        System.out.print(element + " ");
    }// end of enhanced for

    // line break
    System.out.println();

    // printing number of words in array
    System.out.println("Number of words in array: " + quoteArray.length);

    **// printing total number of letters in array**
    for (int counter = 0; counter < quoteArray.length; counter++) {
        String letters = new String(quoteArray[counter]);
    }

    // printing the smallest word

    // printing the biggest word

}// end of main

}// end of class

回答by

int totalCount = 0;
for(String s : quoteArray) {
    totalCount += s.length();
}

回答by user1050632

This is rough code, not ran:

这是粗略的代码,没有运行:

Take the array of strings

取字符串数组

for each value in the array count += string length [i]

对于数组中的每个值 count += string length [i]

taht will count every character of each string in your array.

taht 将计算数组中每个字符串的每个字符。

for finding the smallest string in the array, just keep track of each string length, and compare them, it's a basic search algorithm.

为了找到数组中最小的字符串,只需跟踪每个字符串的长度并进行比较,这是一种基本的搜索算法。

回答by user1050632

Sorry I think i've Worked it out now: 1. Use an int (in this case called total) to keep a running total of letters in each element. 2.use arrayName[counter].length to get length of each individual element. 3. user counter++ to iterate through each each element untill the end of the array.

抱歉,我想我现在已经解决了: 1. 使用 int(在本例中称为 total)来保持每个元素中的字母总数。2.使用 arrayName[counter].length 获取每个单独元素的长度。3. 用户 counter++ 遍历每个元素,直到数组结束。

// printing total number of letters in array
        for (int counter = 0; counter < quoteArray.length; counter++) {
             total +=quoteArray[counter].length();
        }

            System.out.println("Total length of array is: " + total);

回答by Anna Wygant

quoteArray.length will only give you the number of elements in the array (in your case the output would be 13).

quoteArray.length 只会为您提供数组中的元素数(在您的情况下,输出将为 13)。

To get the length of all the strings combined, create a length variable and add to it as you iterate through each array element. Use .length() to get the length of each element and add it to the total length:

要获得所有字符串组合的长度,请创建一个长度变量并在迭代每个数组元素时添加到该变量中。使用 .length() 获取每个元素的长度并将其添加到总长度:

int totalLength = 0;
for(String element : quoteArray){
    totalLength+=element.length();
}
System.out.println("The total length of the quote is: " + totalLength);

回答by Eugene

for

为了

printing total number of letters in array

打印数组中的字母总数

look on java.lang.Character

查看 java.lang.Character

回答by ajb

Computing the number of letters in a string will contain code that looks something like:

计算字符串中的字母数将包含类似于以下内容的代码:

for (int i = 0; i < s.length(); i++)
    if (Character.isLetter(s.charAt(i)))
        // something

where sis the string. charAtreturns the i'th character of the string (where the first character is charAt(0)), and Character.isLettertests whether the character is a letter. I'll let you figure out how to use this and what you might want to use for s.

s字符串在哪里。 charAt返回i字符串的第 ' 个字符(其中第一个字符是charAt(0)),并Character.isLetter测试该字符是否为字母。我会让你弄清楚如何使用它以及你可能想用它来做什么s