1

好的..我是一个完全的 Python 人,很少使用 Java 及其方法。条件是我有一个 Java 函数,我必须向我的导师解释,但我不知道该怎么做……所以如果你们中的一个人能正确阅读,请帮助我分解它并解释它。另外,如果有任何缺陷,我需要找出其操作中的任何缺陷(即循环的使用等)。最后,'string' 和 'string[]' 类型有什么区别?

public static void search(String findfrom, String[] thething){
  if(thething.length > 5){
      System.err.println("The thing is quite long");
  }

  else{
      int[] rescount = new int[thething.length];
      for(int i = 0; i < thething.length; i++){
          String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);
          for(int j = 0; j < characs.length; j++){
              if(characs[j].compareTo(thething[i]) == 0){
                  rescount[i]++;
        }
    }
      }
      for (int j = 0; j < thething.length; j++) {
          System.out.println(thething[j] + ": " + rescount[j]);
      }
  }
}
4

4 回答 4

1
if(thething.length > 5){
      System.err.println("The thing is quite long");
}

如果 的长度String[] thething大于 5。打印错误.. 如果不是,则执行以下 else 块中的内容。

else{

int[] rescount = new int[thething.length];

创建一个新的ints 数组,其大小等于String[] thething

for(int i = 0; i < thething.length; i++)

对于每个索引i,String[] 的东西。

String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);

创建一个名为 characs 的新 String[],它根据正则表达式将 String findfrom 拆分为几个部分"[ \"\'\t\n\b\f\r]"。0 表示此模式将被应用尽可能多的次数。

for(int j = 0; j < characs.length; j++){

现在对于jString[] 字符中的每个索引...

if(characs[j].compareTo(thething[i]) == 0){

比较 characs String[] at index 中的 String 和 thething String[] at indexj中的 String i

如果两者匹配,即该compareTo方法返回 0。

rescount[i]++;

递增int中索引i处的int[] rescount

  for (int j = 0; j < thething.length; j++) {
      System.out.println(thething[j] + ": " + rescount[j]);
  }

最后为每个索引打印出该索引处的字符串和该j索引处的String[] thethingintint[] rescount

而且 aString是一个字符数组,例如String string = "word",aString[]是一个数组字符串。例如String[] strings = new String[]{"word1", "word2",....}.

于 2012-10-04T09:26:19.097 回答
1
public class Test {

    public static void main(String[] args) {
        search(
            "you like me but do you \"like like\" me", 
            new String[]{"you", "like", "me", "not"}
        );
    }

    /**
     * Given a string of words (each word separated by one or more of the
     * following characters: tab, carriage return, newline, single quote, double
     * quote, a form feed, or a word boundary) count the occurrence of each
     * search term provided, with a 5 term limit.
     * 
     * @param findfrom
     *            the string of words
     * @param thething
     *            the search terms.  5 at most, or count will not be performed.
     */
    public static void search(String findfrom, String[] thething) {
        if (thething.length > 5) {
            System.err.println("The thing is quite long");
        }
        else {
            String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);
            int[] rescount = new int[thething.length];
            for (int i = 0; i < thething.length; i++) {
                for (int j = 0; j < characs.length; j++) {
                    if (characs[j].compareTo(thething[i]) == 0) {
                        rescount[i]++;
                    }
                }
            }
            for (int j = 0; j < thething.length; j++) {
                System.out.println(thething[j] + ": " + rescount[j]);
            }
        }
    }
}

输出

you: 2
like: 3
me: 2
not: 0
于 2012-10-04T09:29:54.940 回答
1

该代码的Python版本将是这样的:

import sys
import re

def search(findfrom, thething):
  """Take a string and a list of strings. Printout each of the strings
in the list and a count of how many times that string appeared in the
input string. The string comparison will be based on the input string being
divided up into tokens. The delimiter for each token will be either a whitespace
character, a single quote, or a double quote. """
  if len(thething) > 5:
      sys.stderr.write("The thing is quite long")
  else:
      rescount = [0] * len(thething)
      for i in range(0,len(thething)):
          characs = re.split("[ \"\'\t\n\b\f\r]", findfrom)
          for j in range(0,len(characs)):
              if characs[j] == thething[i]:
                  rescount[i] = rescount[i] + 1

      for j in range(0,len(thething)):
          print thething[j] + ": " + str(rescount[j])


string = 'you like me but do you "like like" me'
strings = ["you", "like", "me", "not"]
search(string,strings)

输出:

you: 2
like: 3
me: 2
not: 0
于 2012-10-04T10:01:57.303 回答
1
  • 第 1 段: findfrom 是一个字符串,它应该是"[ \"\'\t\n\b\f\r]"(正则表达式)分隔的。

  • 第 2 段:thething 是一个字符串数组,最多包含 5 个字符串,该方法将在 findfrom 中查找 'thething' 中的字符串的次数。并将结果打印出来。

例如

findfrom="hello'there'happy'birthday'"
thething={"there","birthday","hello","birthday"}

result would be:
there: 1
birthday: 2
hello: 1
birthday: 2

顺便说一句,这条线

String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);

可能会移出 for 循环。由于 findfrom 没有更改,因此无需重复拆分。

于 2012-10-04T09:15:27.207 回答