About Question enthuware.ocpjp.v7.2.1571 :
Posted: Sun Oct 13, 2013 8:09 am
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));
}
}
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));
}
}