2

I'm trying to calculate how many words an ArrayList contains. I know how to do this if every words is on a separate line, but some of the words are on the same line, like:

hello there
blah
cats dogs

So I'm thinking I should go through every entry and somehow find out how many words the current entry contains, something like:

    public int numberOfWords(){
    for(int i = 0; i < arraylist.size(); i++) {
        int words = 0;
        words = words + (number of words on current line);
    //words should eventually equal to 5
    }
return words;
}

Am I thinking right?

4

3 回答 3

5

int words您应该在循环之外声明和实例化intis 在循环的每次迭代期间不会重新分配。您可以使用for..each语法循环遍历列表,这样就无需将get()项目从列表中移出。要处理一行split中的多个单词Stringinto anArray并计算Array.

public int numberOfWords(){
    int words = 0;
    for(String s:arraylist) {         
      words += s.split(" ").length;
    }
    return words;
}

全面测试

public class StackTest {

    public static void main(String[] args) {
        List<String> arraylist = new ArrayList<String>();
        arraylist.add("hello there");
        arraylist.add("blah");
        arraylist.add("   cats    dogs");
        arraylist.add(" ");
        arraylist.add(" ");
        arraylist.add(" ");

        int words = 0;
        for(String s:arraylist) {
            s = s.trim().replaceAll(" +", " "); //clean up the String
            if(!s.isEmpty()){ //do not count empty strings
               words += s.split(" ").length;
            }
        }
        System.out.println(words);
    }
}
于 2013-04-25T09:11:59.913 回答
1

应该是这样的:

public int numberOfWords(){
    int words = 0;
    for(int i = 0; i < arraylist.size(); i++) {
        words = words + (number of words on current line);
    //words should eventually equal to 5
    }
return words;
}
于 2013-04-25T09:12:30.287 回答
0

我认为这可以帮助你。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class LineWord {

public static void main(String args[]) {
    try {
        File           f        = new File("C:\\Users\\MissingNumber\\Documents\\NetBeansProjects\\Puzzlecode\\src\\com\\test\\test.txt");    // Creating the File passing path to the constructor..!!
        BufferedReader br       = new BufferedReader(new FileReader(f));    //
        String         strLine  = " ";
        String         filedata = "";

        while ((strLine = br.readLine()) != null) {
            filedata += strLine + " ";
        }

        StringTokenizer stk   = new StringTokenizer(filedata);
        List <String>  token = new ArrayList <String>();

        while (stk.hasMoreTokens()) {
            token.add(stk.nextToken());
        }

        //Collections.sort(token);
        System.out.println(token.size());
        br.close();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }
}
}

因此,在这种情况下,您将从文件中删除数据,并在标记它们后将它们存储在列表中,只需计算它们,如果您只想从控制台获取输入,请使用 Bufferedreader,标记它们,用空格分隔,放入列表,简单get size。

希望你得到你正在寻找的东西。

于 2013-04-25T09:49:52.193 回答