Page 1 of 1

About Question enthuware.ocpjp.v7.2.1571 :

Posted: Sun Oct 13, 2013 8:09 am
by DesRenthuware
Hi

I did this:

public int getLen(List<?> al) {

if( al != null) return al.get(0).toString().length();
else return 0;
}

Another alternative (assuming the class is typed - and which will then allow type checking at compile time):

import java.util.*;

class Sub<T> {
public int getLen(List<T> al) {

if( al != null) return al.get(0).toString().length();
else return 0;
}
}

class SubTest {
public static void main(String args[]) {
List<String> str = new ArrayList<>();
str.add("Hello");

Sub<String> ttt = new Sub<>();
System.out.println("Out " + ttt.getLen(str));
}
}

Re: About Question enthuware.ocpjp.v7.2.1571 :

Posted: Fri Jun 09, 2017 5:34 pm
by Cannelids
Gaah, I hate write-in questions: I got this ‘wrong’ by writing “a1” (a-one) instead of “al” (AL). In retrospect I realise the latter is not an unusual identifier as it’s short for ArrayList, but I wasn’t thinking that at the time so didn’t check or copy-paste it. The difference between 1 and lowercase l is almost impossible to discern in the Enthuware font (though not on this forum), so maybe the camelcase aL would be better? (I was one short of a mark I need in an online course because of this :P )

Re: About Question enthuware.ocpjp.v7.2.1571 :

Posted: Sat Jun 10, 2017 11:26 pm
by admin
I have updated it to accept a1 as correct answer as well.

thank you for your feedback!
Paul.

Re: About Question enthuware.ocpjp.v7.2.1571 :

Posted: Sun Jun 11, 2017 4:45 pm
by Cannelids
Thanks :)