从Java中的方法返回ArrayList [英] Return ArrayList from method in Java

查看:344
本文介绍了从Java中的方法返回ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Java中有一个返回列表的方法,它可以返回null:

Hi,

I have a method in Java that returns a List, and it can return null:

public List<Player> route(String message, double[] destination)
{
  List<Player> tempList = new ArrayList<Player>();

 if (.....)
   return null;
 else
  {
     tempList.add(p1);
     tempList.add(p2);
  }

return tempList
}



现在,我想将其添加到我的ArrayList中,但是它不起作用.
我是这样的:



Now I want to add it to my ArrayList, but it doesn''t work.
I do it like this:

myList.addall(route(........);



但这是行不通的. (可能是因为有时我会返回null).

我该怎么办?
我应该如何正确做?

谢谢



but it doesn''t work. (probably because I return null sometimes).

What do I do wrong?
How should I do it correctly?

Thanks

推荐答案

返回一个空的ArrayList:

return an empty ArrayList:

if(condition){
  return new ArrayList<player>();
}
</player>



在接收端处理起来要容易得多.



That is much easier to deal with on the receiving side.


这篇关于从Java中的方法返回ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆