We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;publicclassSolution{publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);// Number of rowsintN=in.nextInt();// Create a 2D ArrayList to store the valueArrayList<ArrayList<Integer>>list=newArrayList<ArrayList<Integer>>();// Initialize the row of the 2D arrayfor(inti=0;i<N;i++){list.add(newArrayList<Integer>());// Number of columnsintC=in.nextInt();// Store the value to each columnfor(intj=0;j<C;j++){list.get(i).add(in.nextInt());}}// Number of queriesintQ=in.nextInt();for(inti=0;i<Q;i++){// Row and Column number from the queriesintrow=in.nextInt();intcol=in.nextInt();/* If queries for the row is larger than N and col in a particular row is also out of bounds then print "Error!" */try{System.out.println(list.get(row-1).get(col-1));}catch(Exceptione){System.out.println("ERROR!");}}in.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Arraylist
You are viewing a single comment's thread. Return to all comments →