Untitled

 avatar
unknown
plain_text
a year ago
1.8 kB
4
Indexable
package JavaCollection;

import java.util.ArrayList;
import java.util.Scanner;

public class NumbersArrayList {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter total number of lines:");
        int lines = scanner.nextInt();

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

        for (int i = 0; i < lines; i++) {
            System.out.print("Enter the number of elements for line " + (i + 1) + ":");
            int numElements = scanner.nextInt();

            //if(numElements!=0) {
            ArrayList<Integer> row = new ArrayList<>();
            System.out.print("Enter the elements for line " + (i + 1) + ":");
            for (int j = 0; j < numElements; j++) {
                int element = scanner.nextInt();
                row.add(element);
            }
            //  System.out.println(row);
            rows.add(row);
            //}
        }
        System.out.println(rows);

        System.out.print("Enter the no of queries:");
        int queryCount = scanner.nextInt();
        for (int m = 1; m <= queryCount; m++) {
            System.out.print("Enter the line to be fetched:");
            int queryLine = scanner.nextInt();
            System.out.print("Enter the position to be fetched:");
            int queryPosition = scanner.nextInt();
            System.out.println("line size:" + rows.get(queryLine - 1).size());

            if (queryPosition - 1 < rows.get(queryLine - 1).size()) {
                System.out.println(rows.get(queryLine - 1).get(queryPosition - 1));
            } else {
                System.out.println("ERROR!");
            }
        }
    }
}

Editor is loading...
Leave a Comment