【问题标题】:How to acces the value of an ArrayList of another class? [duplicate]如何访问另一个类的 ArrayList 的值? [复制]
【发布时间】:2018-05-27 18:06:12
【问题描述】:

我有一个带有 arrayList 的类,我创建了一个方法 (getList) 来返回它的值。

ArrayList<String> data = new ArrayList<>();

public void Tes2(){
    data.add("aku");
    data.add("sudah");
    data.add("mandi");

}

public ArrayList getList(){
    return data;
}

然后我有另一个类将从上一个类中调用 arrayList。

public static void main(String[] args) {
    Tes2 ambil = new Tes2();
    ambil.getList();
    System.out.println(ambil.getList()+" ");
}

但是当我编译代码时,没有打印任何内容。我不知道出了什么问题,就像我的数组列表是空的。我怎样才能做到正确?

【问题讨论】:

  • 你有一个“伪”构造函数。摆脱void
  • 例如,将 public void Tes2(){ 更改为 public Tes2(){

标签: java arraylist


【解决方案1】:

要么删除构造函数中的 void,要么将其更改为其他名称(例如 Loadlist),使其成为方法。然后从你的主类中你可以调用 ambit.Loadlist() 来加载列表,然后你可以编写 ambil.getList()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    相关资源
    最近更新 更多