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.Arrays;importjava.util.List;importjava.util.Scanner;importstaticjava.util.stream.Collectors.toList;publicclassSolution{privatestaticfinalStringSTRING_INSERT="Insert";privatestaticfinalStringSTRING_DELETE="Delete";publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intn=Integer.parseInt(scanner.nextLine());// n is the number initial of elements in the listList<Integer>list=getIntegerList(scanner.nextLine());intnumberOfQuerys=Integer.parseInt(scanner.nextLine());while(numberOfQuerys-->0){Stringquery=scanner.nextLine();List<Integer>queryList=getIntegerList(scanner.nextLine());if(query.equals(STRING_INSERT)){list.add(queryList.get(0),queryList.get(1));}elseif(query.equals(STRING_DELETE)){list.remove((int)queryList.get(0));// int -> index to delete, Integer -> object to delete}}scanner.close();list.forEach(number->System.out.print(number+" "));}privatestaticList<Integer>getIntegerList(Stringscanner){returnArrays.stream(scanner.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 List
You are viewing a single comment's thread. Return to all comments →
Use stream