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));
}
}
About Question enthuware.ocpjp.v7.2.1571 :
Moderators: Site Manager, fjwalraven
-
- Posts: 32
- Joined: Wed Aug 28, 2013 6:12 am
- Contact:
-
- Posts: 25
- Joined: Thu Jun 01, 2017 2:50 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1571 :
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 )
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1571 :
I have updated it to accept a1 as correct answer as well.
thank you for your feedback!
Paul.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 25
- Joined: Thu Jun 01, 2017 2:50 pm
- Contact:
Who is online
Users browsing this forum: No registered users and 61 guests