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.
About Question enthuware.ocpjp.v7.2.1624 :
Moderator: admin
-
- Posts: 16
- Joined: Fri Sep 04, 2015 5:06 pm
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
No, sa can never have objects other than Strings.
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Fri Sep 04, 2015 5:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
I mean Animal[] sa= {new Animal("dog"), new Animal("cat"), new Animal("elephant")}
where Animal is a valid class with constructor that takes string
where Animal is a valid class with constructor that takes string
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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[]?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1624 :
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 80 guests