简体   繁体   English

如何使用2D ArrayList?

[英]How to use 2d ArrayList?

I want syntax for creating 2d ArrayList and how to take input in it one by one. 我想要创建2d ArrayList的语法以及如何在其中一一输入的语法。

ArrayList<ArrayList<Integer>> list = new ArrayList<>();

Is this syntax is right? 这种语法正确吗?

Yes, Your syntax is absolutely right. 是的,您的语法绝对正确。 You can take input using Scanner class. 您可以使用Scanner类进行输入。 Let me brief you an example. 让我向您介绍一个例子。

Scanner scanner=new Scanner(System.in);
    int row = scanner.nextInt();
    int col = scanner.nextInt();
    ArrayList<Integer> list=null;
    for (int j = 0; j < row; j++) {
        for (int i = 0; i < col; i++) {
            list = new ArrayList<>();
            list.add(scanner.nextInt());
        }
        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
        lists.add(list);
    }

Try this out. 试试看 Hope it works :) 希望它能工作:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM