简体   繁体   English

如何在主 class 双列表中导入另一个 class?

[英]How can I import another class in main class double list?

I'm beginner programmer.我是初级程序员。 I made a double list in an external class, and I want to print it out in the main class.我在外部class做了一个双表,想在主class打印出来。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class A {

    private List<List<Object>> A=new ArrayList<>();

    public List<List<Object>> getA() {
        return A;
    }

    public void setA(List<List<Object>> A) {
        this.A.add(new ArrayList(Arrays.asList("1+1")));
        this.A.add(new ArrayList(Arrays.asList("dog")));
    }
}

In this class, I made double list and I will put this double list to main class在这个 class 中,我制作了双重列表,我将把这个双重列表放到主 class

public class Main {
    public static void main(String[] args) {    
        A a=new A();    
        System.out.println(a.getA());       //this result []
    }
}

I want to print[[1+1],[dog]] , but it does show [] .我想print[[1+1],[dog]] ,但它确实显示[]

What is problem??什么问题??

I want to print the desired result.我想打印所需的结果。

You never actually call your setA method from your main class.您实际上从未从主 class 调用 setA 方法。

public class Main {
public static void main(String[] args) {    
    A a=new A();   
    a.setA(a); // <-- populate array list here 
    System.out.println(a.getA());       //this result []
}

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

相关问题 如何将主 class 中的变量导入另一个 class - how to import a variable in Main class to a another class 我怎样才能在另一个班级访问这个列表? - How can I access this list in another class? 我如何从另一个模块Maven导入类 - how can i import class from another module maven Minecraft Bukkit:如何从主类访问另一个类的属性/方法? - Minecraft Bukkit: How can I access properties/methods in another class from the main class? 如何在Java中使用TestNG将字符串从主类传递到另一个类? - How can I pass a string from main class to another class using TestNG in java? 如何检测Java类是由其自己的main()调用还是从另一个类调用? - How can I detect if a Java class is called by its own main() or from another class? 我如何从主类调用textArea以便我可以在另一个类中使用它? - How do iI call textArea from main class so that I can use it in another class? 如何在另一个类中调用一个类的main()方法? - How do I invoke a main() method of one class in another class? 如何获取主类的Java类依赖项列表? - How do I get a list of Java class dependencies for a main class? 如何将main包装在可扩展类中? - How can I wrap main in an extendable class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM