Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.4 kB
1
Indexable
Never
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<>();
        System.out.println("Enter the element count and elements per line");
        for (int i = 0; i < lines; i++) {
          
            int numElements = scanner.nextInt();
            ArrayList<Integer> row = new ArrayList<>();
            for (int j = 0; j < numElements; j++) {
                int element = scanner.nextInt();
                row.add(element);
            }
            rows.add(row);
        }

        System.out.print("Enter the query count:");
        int queryCount = scanner.nextInt();
        for (int m = 0; m < queryCount; m++) {
           System.out.print("Enter line and position of element to be found");
            int queryLine = scanner.nextInt();
            int queryPosition = scanner.nextInt();
            if (queryPosition - 1 < rows.get(queryLine - 1).size()) {
                System.out.println(rows.get(queryLine - 1).get(queryPosition - 1));
            } else {
                System.out.println("ERROR!");
            }
        }
    }
}

Leave a Comment