I'm solving this problem in Hackerrank. How can i use instanceof
operator in Java to successfully compile the code?
Link : https://www.hackerrank.com/challenges/java-iterator
The incomplete code :
import java.util.*;
public class Main
{
static Iterator func(ArrayList mylist)
{
Iterator it=mylist.iterator();
while(it.hasNext())
{
Object element = it.next();
if(~~Complete this line~~)//Hints: use instanceof operator
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);
}
}
}
Even though i got it done by using if(element == "###")
, please guide me on how to use instanceof
operator at that place.
Edit : That thread couldn't provide answers as to how to use that instanceof operator to check for the instance of a special character group like "###" in a ArrayList. Please help me understand. Sorry if I'm violating any rule here.