0

I want syntax for creating 2d ArrayList and how to take input in it one by one.

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

Is this syntax is right?

1
  • 1
    More or less. Have got tried it? That's the easiest way to check. Commented Nov 12, 2017 at 16:21

1 Answer 1

1

Yes, Your syntax is absolutely right. You can take input using Scanner class. 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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.