How to return multiple objects from a Java method?

前端 未结 25 3506
眼角桃花
眼角桃花 2020-11-21 23:55

I want to return two objects from a Java method and was wondering what could be a good way of doing so?

The possible ways I can think of are: return a HashMap<

25条回答
  •  深忆病人
    2020-11-22 00:30

    Pass a list to your method and populate it, then return the String with the names, like this:

    public String buildList(List list) {
        list.add(1);
        list.add(2);
        list.add(3);
        return "something,something,something,dark side";
    }
    

    Then call it like this:

    List values = new ArrayList();
    String names = buildList(values);
    

提交回复
热议问题