【问题标题】:Trying to call an ArrayList from another method尝试从另一个方法调用 ArrayList
【发布时间】:2018-11-07 01:09:42
【问题描述】:

所以,我有 2 个方法,每个方法返回一个相同类型的业务层对象类的列表。

我正在尝试创建一个新方法来创建一个同样类型的新 ArrayList 并将这两个列表添加到其中。

有人知道如何将这些列表调用到该组合列表中吗?这似乎不起作用。

public response getlist1() {
    ....

    return list1;
}

public response getlist2() {
    ....

    return list2;
}

public response getCombinedList() {
     ArrayList<...> = new ArrayList<...>();     
     combinedList.add(list1);                                                
     combinedList.add(list2);
     ...   
}

【问题讨论】:

  • 使用combindedList.addAll(list1);
  • 你在哪里调用 getlist1() 和 getlist2()?
  • list1 替换为getList1(),将list2 替换为getList2()
  • 您可能希望复习变量范围(实例、类、本地)的概念。如果找不到list1list1,它们可能超出了getCombinedList 的范围。

标签: java arraylist methods


【解决方案1】:

在这种情况下,我会使用列表的addAll 方法:

public response getCombinedList(){
     ArrayList<...> = new ArrayList<...>();     
     combinedList.addAll(list1);                                                
     combinedList.addAll(list2);    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2012-07-03
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多