Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Java Iterator-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Java Iterator – Hacker Rank Solution
Java Iterator – Hacker Rank Solution
Problem :
Java Iterator class can help you to iterate through every element in a collection. Here is a simple example:
import java.util.*; public class Example{ public static void main(String []args){ ArrayList mylist = new ArrayList(); mylist.add("Hello"); mylist.add("Java"); mylist.add("4"); Iterator it = mylist.iterator(); while(it.hasNext()){ Object element = it.next(); System.out.println((String)element); } } }
In this problem you need to complete a method func. The method takes an ArrayList as input. In that ArrayList there is one or more integer numbers, then there is a special string “###”, after that there are one or more other strings. A sample ArrayList may look like this:
element[0]=>42 element[1]=>10 element[2]=>"###" element[3]=>"Hello" element[4]=>"Java"
You have to modify the func method by editing at most 2 lines
so that the code only prints the elements after the special string “###”. For the sample above the output will be:
Hello Java
Note: The stdin doesn’t contain the string “###”, it is added in the main method.
To restore the original code in the editor, click the top left icon on the editor and create a new buffer.
Java Iterator – Hacker Rank Solution
import java.util.*; public class Main { static Iterator func(ArrayList mylist) { Iterator it=mylist.iterator(); while(it.hasNext()) { Object element = it.next(); if(element instanceof String) break; } return it; } public static void main(String []argh) { ArrayList mylist = new ArrayList(); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); for(int i=0;i<n;i++) { mylist.add(sc.nextInt()); } mylist.add("###"); for(int i=0;i<m;i++) { mylist.add(sc.next()); } Iterator it=func(mylist); while(it.hasNext()) { Object element = it.next(); System.out.println((String)element); } } }
superb post.Never knew this, appreciate it for letting me know.