Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java Arraylist problem solution

YASH PAL, 31 July 2024

In this HackerRank java Arraylist problem in java programming language You are given n lines. In each line, there are zero or more integers. You need to answer a few queries where you need to tell the number located in the Yth position of the Xth line.

HackerRank Java Arraylist problem solution

HackerRank Java Arraylist problem solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan=new Scanner(System.in);

    int total=scan.nextInt();

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

    for(int i=0;i<total;i++)
    {
     int size=scan.nextInt();
     ArrayList<Integer> list=new ArrayList();

     for(int j=0;j<size;j++)
       {
          int item=scan.nextInt();
          list.add(item);
       }
      mainlist.add(list);
   }

    int totalprint=scan.nextInt();
    for(int k=0;k<totalprint;k++)
    {
      int row=scan.nextInt();
      int col=scan.nextInt();
      try
       {
       System.out.println(mainlist.get(row-1).get(col-1));
       }
      catch(Exception e)
      {
      System.out.println("ERROR!");
      }
    }
    scan.close();
    }
}

Second solution

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;


public class Solution
{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 64 * 1024);

        final int N = Integer.parseInt(br.readLine().trim(), 10);

        final List<List<Integer>> arr = new ArrayList<List<Integer>>();

        for (int i = 0; i < N; i++) {
            final String[] data = br.readLine().trim().split(" ");
            final int D = Integer.parseInt(data[0], 10);
            final List<Integer> a = new ArrayList<Integer>();
            for (int j = 1; j <= D; j++) {
                final int v = Integer.parseInt(data[j], 10);
                a.add(v);
            }
            arr.add(a);
        }

        final StringBuilder sb = new StringBuilder();
        final int Q = Integer.parseInt(br.readLine().trim(), 10);
        for (int i = 0; i < Q; i++) {
            final String[] query = br.readLine().trim().split(" ");
            final int r = Integer.parseInt(query[0], 10);
            final int c = Integer.parseInt(query[1], 10);
            if (r > arr.size()) {
                sb.append("ERROR!n");
            } else {
                final List<Integer> row = arr.get(r - 1);
                if (c > row.size()) {
                    sb.append("ERROR!n");
                } else {
                    sb.append(row.get(c - 1)).append('n');
                }
            }
        }

        System.out.print(sb.toString());

        br.close();
        br = null;
    }
}

The solution in java8 programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        ArrayList<ArrayList< Integer >> arr = new ArrayList<ArrayList< Integer >>();        
        
        Scanner sc = new Scanner(System.in);
        
        int N = sc.nextInt();
        for (int n=0 ; n<N ; ++n) {
            
            int D = sc.nextInt();
            ArrayList< Integer > a = new ArrayList< Integer >();
            
            for (int d=0 ; d<D ; ++d) {
                a.add(sc.nextInt());   
            }
            
            arr.add(a);            
        }
        
        int Q = sc.nextInt();
        for (int q=0 ; q<Q ; ++q) {
            
            int x = sc.nextInt();
            int y = sc.nextInt();
            
            try {
                System.out.println(arr.get(x-1).get(y-1));
            } catch (Exception e) {
                System.out.println("ERROR!");
            }
        }
    }
}
coding problems solutions Hackerrank Problems Solutions Java Programming Tutorials

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes