You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
class Student{} class Rockstar{} class Hacker{}
public class Solution {
static String count(ArrayList mylist){ int a = 0,b = 0,c = 0; for(int i = 0; i < mylist.size(); i++){ Object element=mylist.get(i); if(element instanceof Student) a++; if(element instanceof Rockstar) b++; if(element instanceof Hacker) c++; } String ret = a + " " + b + " " + c; return ret; } public static void main(String[] args) { ArrayList mylist = new ArrayList(); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0; i<t; i++){ String s=sc.next(); if(s.equals("Student")) mylist.add(new Student()); // Added this line if(s.equals("Rockstar")) mylist.add(new Rockstar()); // Added this line if(s.equals("Hacker")) mylist.add(new Hacker()); // Added this line } System.out.println(count(mylist)); }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java Instanceof keyword
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
class Student{} class Rockstar{} class Hacker{}
public class Solution {
}