java 从对象数组列表调用方法

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

Calling a method from an array-list of objects

javamethodsstaticarraylistnon-static

提问by tom3322

I'm just starting with Java and I'm having trouble with the following code. I was using something like this to call the non-static apply method from a static method, but I dont think its very efficient. I set an array list of the rules that need to be applied but I can't get it to work.

我刚开始使用 Java,但在使用以下代码时遇到了问题。我正在使用这样的东西从静态方法调用非静态应用方法,但我认为它不是很有效。我设置了需要应用的规则的数组列表,但我无法让它工作。

    ClassificationRule rules = new RuleFirstOccrnc();
    ClassificationRule rules1 = new RuleOccrncCount();
    rules.apply(aUserInput);
    rules1.apply(aUserInput); 

I'm getting this error when trying to call the apply() method from ClassificationRule "The method apply(String) is undefined for the type ArrayList". Any help would be greatly appreciated!

尝试从 ClassificationRule 调用 apply() 方法时出现此错误“方法 apply(String) 未定义为类型 ArrayList”。任何帮助将不胜感激!

package tweetClassification;

import java.util.ArrayList;

public class PrioritRuls {

    //Set of rules to be applied
    final static ArrayList<ClassificationRule> rulesA
        = new ArrayList<ClassificationRule>();

    static{
        rulesA.add( new RuleFirstOccrnc() );
        rulesA.add( new RuleOccrncCount() );
    }

    // ******************************************* 
    public static void prioritize( final String aUserInput ){

        rulesA.apply(aUserInput); //ERROR
       // The method apply(String) is undefined
       // for the type ArrayList<ClassificationRule>
        }
} 
package tweetClassification;

public class ClassificationRule {

     // *******************************************
     public void apply (final String aUserInput) {  

        apply( aUserInput );
        }
}

回答by Chris Thompson

Right, because you're calling the applymethod on the array list object, not the contents of the array list.

是的,因为您正在调用apply数组列表对象上的方法,而不是数组列表的内容。

Change it to something like

将其更改为类似

rulesA.get(0).apply()

Or, if you want to call it on every element, you need to iterate through the list.

或者,如果你想在每个元素上调用它,你需要遍历列表。

for (ClassificationRule rule:rulesA){
  rule.apply(aUserInput);
}

回答by amit

You are trying to invoke apply()on the ArrayListinstead on the ClassificationRuleobject. ArrayListdoes not have this method, so as expected - you get a compilation error.

您正在尝试调用apply()ArrayList,而不是上ClassificationRule对象。ArrayList没有这个方法,所以正如预期的那样 - 你会得到一个编译错误。

You might want to iteratethe ArrayListand apply()on each ClassificationRuleobject with a for-each loop:

您可能要迭代ArrayListapply()每一个上ClassificationRule有一个对象for-each循环

for (ClassificationRule rule : rulesA) rule.apply(aUserInput)

or to apply()on a specific element:

apply()特定元素上

rulesA.get(someIndex).apply(aUserInput)

One more thing:

还有一件事:

public void apply (final String aUserInput) {  
   apply( aUserInput );
}

will cause an infinite recursivecalls to apply()[well, not exactly infinite, it will throw eventually an exception]. This is not the error you are currently having, since this is run-time error, while you are still stuck at compile-time errors.

将导致无限递归调用apply()[好吧,不是完全无限,它最终会抛出异常]。这不是您当前遇到的错误,因为这是运行时错误,而您仍然陷入编译时错误。