Page 1 of 1
About Question enthuware.ocpjp.v7.2.1624 :
Posted: Sun Sep 06, 2015 5:56 pm
by boyonsea
Just wanted to note that,
If the array 'sa' has objects other than String that don't implement comparable, java.lang.ClassCastException will be thrown at runtime.
Re: About Question enthuware.ocpjp.v7.2.1624 :
Posted: Sun Sep 06, 2015 7:58 pm
by admin
No, sa can never have objects other than Strings.
Re: About Question enthuware.ocpjp.v7.2.1624 :
Posted: Sun Sep 06, 2015 10:40 pm
by boyonsea
I mean Animal[] sa= {new Animal("dog"), new Animal("cat"), new Animal("elephant")}
where Animal is a valid class with constructor that takes string
Re: About Question enthuware.ocpjp.v7.2.1624 :
Posted: Thu Feb 20, 2020 11:59 am
by bvrulez
What I don't understand is why "sa" is effected by "Comparator.sort(Arrays.asList(sa), null)". I would expect "Arrays.asList(sa)" to generate a NEW ArrayList (or something similarly appropriate extending List) which will get lost because it is not saved to a reference variable. Why does "Arrays.asList()" sort the underlying String[]?
Re: About Question enthuware.ocpjp.v7.2.1624 :
Posted: Thu Feb 20, 2020 12:10 pm
by admin
Because the list is backed by the array as explained in the [url=
https://docs.oracle.com/javase/8/docs/a ... l#asList-T...-]JavaDoc API description of Arrays.asList method[/b]. Anything you do to the list actually happens on the underlying array.
Re: About Question enthuware.ocpjp.v7.2.1624 :
Posted: Thu Feb 20, 2020 12:12 pm
by admin
One important habit that the part 1 exam (i.e OCA) inculcates is to read the JavaDoc. That should be your first source of information.