2012-10-04 41 views
1

好的..我是一个Python总人,很少与Java及其方法一起工作。条件是我得到了一个Java函数,我必须向我的导师解释,而我也不知道如何去做。如果你们其中一个人可以正确阅读这个,请帮助我分解它并解释它。另外,如果有任何问题,我需要找出其操作中的任何缺陷(即使用循环等)。最后,'string'和'string []'类型有什么区别?Java函数分析

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]); 
     } 
    } 
} 
+0

回答“finally”问题:String是一个字符数组,String []是一个字符串数组。 – azendh

+0

'dataType variableName' - 定义数据类型(dataType)的单个变量(variableName)。 'dataType [] variableName' - 定义数据类型(dataType)的数组变量(variableName)。 其中大部分代码都是自我解释的,''thething.length'是东西的长度。如果你自己仔细阅读代码并且说出你不明白的东西(我对Python不熟悉,所以不知道它与Java有多不同),会更容易。 –

回答

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没有改变,所以不需要重复分割。

+0

谢谢。这很有帮助。 – khan

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

如果String[] thething的长度大于5.打印错误.. 如果没有做什么其他如下块内。

else{

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

与大小等于所述String[] thething

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

长度对于每个索引i字符串[] thething创建的int秒的新的数组。

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

创建一个新的String []所谓characs,其将字符串findfrom到基于正则表达式"[ \"\'\t\n\b\f\r]"几个部分。 0表示这种模式将尽可能多次应用。

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

现在对于在String [] characs每个索引j ...

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

比较在characs字符串的String []在索引i thething的String []指数j与字符串。

如果两者匹配即所述compareTo方法返回0

rescount[i]++; 

逐个增加int即在int[]rescount索引i

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

最后在String[]thething打印出该索引处的字符串的每个索引j并在int[]rescount

,并且也是Stringint该索引处是字符数组,例如String string = "word"和一个String[]是一个数组字符串。例如String[] strings = new String[]{"word1", "word2",....}.

+0

谢谢,伙计。它帮助了很多。 – khan

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 
+0

谢谢joe ...你看到这段代码中的任何缺陷?我的意思是任何可以修改以增强功能的东西? – khan

+0

除了肯特所说的,关于移动内部循环之外的.split()。 –

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 
+0

谢谢滑板车。 – khan