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.util.List;importjava.util.Scanner;importjava.util.stream.Stream;importstaticjava.util.stream.Collectors.toList;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intnumberOfArrays=getInputNumber(scanner);intnumberOfQuerys=0;List<List<Integer>>arraysOfNumbers=newArrayList<>();List<List<Integer>>arrayOfQuery=newArrayList<>();// x , y in every queryfor(inti=0;i<numberOfArrays;i++){List<Integer>list=getInputListNumbers(scanner);list.remove(0);arraysOfNumbers.add(list);}numberOfQuerys=getInputNumber(scanner);while(numberOfQuerys-->0){arrayOfQuery.add(getInputListNumbers(scanner));}scanner.close();arrayOfQuery.forEach(list->{intx=list.get(0);// position in array of numbersinty=list.get(1);// position in arrayprintNumber(x,numberOfArrays,arraysOfNumbers,y);});}privatestaticvoidprintNumber(intx,intnumberOfArrays,List<List<Integer>>arraysOfNumbers,inty){if(x<0||x>numberOfArrays){System.out.println("ERROR!");}List<Integer>array=arraysOfNumbers.get(x-1);try{System.out.println(array.get(y-1));}catch(Exceptione){System.out.println("ERROR!");}}privatestaticintgetInputNumber(Scannerscanner){returnInteger.parseInt(scanner.nextLine());}privatestaticList<Integer>getInputListNumbers(Scannerscanner){returnStream.of(scanner.nextLine().split(" ")).map(Integer::parseInt).collect(toList());}}
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 →