About Question enthuware.ocpjp.v8.2.1309 :
Posted: Sun Oct 09, 2016 12:42 pm
Hello, I've been trying to complete this question with less type parameters, I know is not the best answer but my code compiles and runs without errors or warnings while Enthuware tells me is wrong. I've got different right answers but this one is not working
Code: Select all
private <E extends CharSequence> Collection<CharSequence> getWordsStartingWith(
Collection<E> input, char ch) {
Collection<CharSequence> returnValue = new ArrayList<CharSequence>();
int len = input.size();
for (E e : input) {
if (e.charAt(0) == ch)
returnValue.add(e);
}
return returnValue;
}
public void checkIt() {
List<String> a = new ArrayList<String>();
a.add("apple");
a.add("cherry");
Set<StringBuffer> b = new HashSet<StringBuffer>();
b.add(new StringBuffer("pineapple"));
Collection<CharSequence> ac = getWordsStartingWith(a, 'a');
Collection<CharSequence> bc = getWordsStartingWith(b, 'a');
System.out.println(ac);
System.out.println(bc);
}