2017-05-04 37 views
0

寻找一个递归的手,我知道这是一个简单的问题,但在某处退出但不知道如何/在哪里!Java - 递归地统计列表中的单词的出现

这里是我的递归方法:

public static int getNumAppearances(myList<String> l, String word) 
{ 
    int index = 0; 
    int count = 0; 
    String search = word; 

    if(index > l.my_get_length()-1) 
    { 
     return 0; 
    } 
    else if(l.my_get_element(index).equals(search)) 
    { 
     count++; 
     index++; 
    } 

    return count + getNumAppearances(l, word); 
} 

干杯!

编辑,myList中类:

public interface myList<T> { 

    //------------------------------------------------------------------- 
    // Create an empty MyList: create_empty 
    //------------------------------------------------------------------- 
    //public myList create_empty(); --> Java does not support constructors in interfaces 

    //------------------------------------------------------------------- 
    // Basic Operation --> Get number of elements in MyList: my_get_length 
    //------------------------------------------------------------------- 
    public int my_get_length(); 

    //------------------------------------------------------------------- 
    // Basic Operation --> Get element at of MyList at a concrete position: my_get_element 
    //------------------------------------------------------------------- 
    public T my_get_element(int index) throws myException; 

    //------------------------------------------------------------------- 
    // Basic Operation --> Add element to MyList at a concrete position: my_add_element 
    //------------------------------------------------------------------- 
    public void my_add_element(int index, T element) throws myException; 

    //------------------------------------------------------------------- 
    // Basic Operation --> Remove element of MyList at a concrete position: my_remove_element 
    //------------------------------------------------------------------- 
    public void my_remove_element(int index) throws myException; 

} 

我意识到你需要的理想传递给方法的指数但不幸的是,这不是他有它设置方式!

+2

请注明您所遇到的问题。 –

+0

递归执行此操作没有理由。只需循环查看您的列表并查看单词。 – AndyB

+0

@AndyB很可能这是一个家庭作业任务,旨在教授递归 - 因此不允许使用更明智的方法 – Catchwa

回答

0

您可以通过修改列表,这样在你的函数计算:

public class RecursiveListWordCount { 

    public static void main(String[] args) { 
     System.out.println(count(Arrays.asList("a", "b", "a", "b", "c"), "d")); 
    } 

    public static final int count(List<String> list, String word) { 
     if(list.isEmpty()) { 
      return 0; 
     } 

     if(list.get(0).equals(word)) { 
      return 1 + count(list.subList(1, list.size()), word); 
     } else { 
      return 0 + count(list.subList(1, list.size()), word); 
     } 

    } 
} 

在每次调用,我检查,如果该列表是空的,如果true我将返回0(为空列表肯定没有它可能是相同的单词)。

接下来的调用将添加一个子列表,删除我刚刚检查的单词。

希望帮助,

阿图尔

+0

不幸的是我无法修改列表,它必须保持原样! –

+0

上述代码不会修改您的原始列表。它会创建一份您的清单的副本并将其传递。一旦递归函数返回,您的列表将仍然具有相同顺序的相同项目并且是未修改的。除了将索引作为字段而不是局部变量进行计数,但这将是一个可怕的解决方案:)) – pandaadb

+0

虽然我只有4种方法可用,但my_get_length(),my_remove_element(),my_get_element() )和my_add_element。,不能使用。subList等 –

0

看来你利用index变量不正确,因为你总是检查每一个方法调用本身相同时间index,我会建议使用index作为参数传递给方法。而不是保留count变量,我们可以在每次找到匹配时将结果添加到1

public static int getNumAppearances(List<String> list, String word, int index) 
{  if(list == null || list.size() == 0 || index < 0) return -1; // you can throw an exception instead if you deem it necessary. 
     if(index > list.size() - 1) return 0; 
     else if(list.get(index).equals(word)) return 1 + getNumAppearances(list, word, index + 1); 
     return getNumAppearances(list, word, index + 1); 
} 

- 调用方法时,一定要通过0作为参数传递给index参数,因为这种方法检查从开始到结束的名单。

+0

干杯,不幸的是我不能改变他希望我们使用的方法,所以我不能给它添加一个Index参数。 –

相关问题