简体   繁体   English

如何为该方法返回值?

[英]How can i return value for this method?

I have a method which returns the value of my POJO. 我有一个返回POJO值的方法。

My POJO class's name is Tail . 我的POJO班的名字是Tail

public Tail getTail(long value1, String value2, int value3) {

    List<Tail> l = /*I get this list via Hibernate. */
    if (l.size() == 1) {
        return (Tail) l.get(0);
    } 
    else if (l.size() > 1) {
        for (Tail t : l) {
            First ik = minorDAO.getFirst(value3, t.getNumber());
            if( ik.getCond().equals("I") ){
                t.setCond("I");
                continue;
            } else {
                return t;   //???????????????               
            }   
        }               
    } else {
        return null;
    }           
}   

In the else statement inside of the foreach loop where the question mark comment is I want to return t, but if I do this, I get an error stating: 在foreach循环内的问号注释所在的else语句中,我想返回t,但是如果执行此操作,则会出现错误,指出:

"This method must return a result of type Tail". “此方法必须返回Tail类型的结果”。

How can I return the value there? 如何在那里返回值?

with that code you dont get the message: 使用该代码,您不会收到以下消息:

public Tail getTail(long value1, String value2, int value3) {
        List<Tail> l = --> (I get list via Hibernate.)
        if (l.size() == 1) {
            return (Tail) l.get(0);
        } else if (l.size()>1){
            for (Tail t: l) {
                First ik = minorDAO.getFirst(value3, t.getNumber());
                if( ik.getCond().equals("I") ){
                    t.setCond("I");
                    continue;
                } else {
                    return t;
                }   
            }

        }
        return null;

    }

If the method has to return Tail you must deliver it in any possible case. 如果该方法必须返回Tail ,则在任何可能的情况下都必须传递它。 For example: 例如:

if each element is true in this case: 如果在这种情况下每个元素都为true ,则:

if( ik.getCond().equals("I") ){

there would be no return. 不会有回报。 You iterate about the whole list and nothing happens... 您遍历整个列表,什么也没有发生...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM