About Question enthuware.ocajp.i.v7.2.850 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javaman
Posts: 33
Joined: Wed Nov 13, 2013 4:11 pm
Contact:

About Question enthuware.ocajp.i.v7.2.850 :

Post by javaman »

You must specify the class of objects you want to store in ArrayList when you declare a variable of type ArrayList.
This is not true because you can still use non-generic form. For example, instead of using ArrayList<String> listOfStrings; you can use: ArrayList listOfStrings; Of course, if you use non generic version, you will lose the compile time type checking.

Is this illustrated b y the following

import java.util.ArrayList;

class LearnJava{
public static void main(String args[]){
ArrayList<String> al = new ArrayList<>();
al.add("0");
a1.add(1);
ArrayList al2 = new ArrayList();
al2.add("0");
al2.add(1);
for(Object i : al2){
System.out.println(i);
}
}
}

a1.add(1); gives a compile error because I declared the ArrayList to contain Strings and 1 is an integer. Compiler is making sure I add what I specified.
Since I do not specify the type of element al2 can contain I have to use a loopvariable of type Object in for(Object i : al2) and therefore lose the item-specific functions since loopvariable i is of type Object instead of e.g. String?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by admin »

You can only store objects (not primitives) in an ArrayList that is why add(1); fails to compile. The rest is correct i.e. you can "use a loopvariable of type Object in for(Object i : al2) and therefore lose the item-specific functions since loopvariable i is of type Object instead of e.g. String"

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Vermeulen
Posts: 12
Joined: Wed Jul 15, 2015 4:05 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by Vermeulen »

admin wrote:You can only store objects (not primitives) in an ArrayList that is why add(1); fails to compile.
True but due to autoboxing, add(1); compiles fine for ArrayLists that can contain Integers, i.e. a raw ArrayList, ArrayList<Object>, ArrayList<Number>, and ArrayList<Integer>.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by sir_Anduin@yahoo.de »

Hi,

where does the method .sort() comes from.
I have checked the collection interface doc, but there is no such method:

http://docs.oracle.com/javase/7/docs/ap ... ction.html

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by admin »

The sort method is in Collections class not Collection interface.
If you like our products and services, please help us by posting your review here.


flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by flex567 »

Collections class is never mentioned in the book. Do we need to know anything about it for the exam?

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by admin »

No, Collections class is not required for the exam. But some candidates have reported seeing it mentioned in the options in relation to sorting an array list. So, it will be good if you know that Collections.sort can be used to sort an ArrayList. Nothing more is required.
If you like our products and services, please help us by posting your review here.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by Dreamweaver »

It allows you to access its elements in random order.
This question is very ambiguity because get(index) method return the element at a specified position (index) in the list not random

admin
Site Admin
Posts: 10065
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by admin »

No, "ability to access elements in random order" is a standard industry/computer science term. It is well understood everywhere.
See this discussion: https://stackoverflow.com/questions/327 ... -behaviour
If you like our products and services, please help us by posting your review here.

Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.850 :

Post by Dreamweaver »

ok, Thank you for explanation.

Post Reply

Who is online

Users browsing this forum: No registered users and 123 guests