[HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

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

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by OCAJO1 »

al.add("charlie"); //insert an object
al.add(2, "david"); //remove an object
al.remove(0); //process the objects
Although obvious by the code, but maybe the above comments should be adjusted.

Also, a side question to different part of the same code segment,
for(Object o : al){
String name = (String) o;
System.out.println(name+" "+name.length());
}
given the above code,

1. Why the following causes compiler error "variable declaration not allowed here" only when no brackets are present.
for(Object o : al)
String name = (String) o;
2. while, as expected, the following causes no compiler error with or without brackets.
String name = " ";
for(Object o : al)
System.out.println(name+" "+name.length());
Thanks

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

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by admin »

Added to errata. Thank you for reporting.

1/2. That is just a rule of the Java language. As per JLS 14.14.1., the basic for statement allows "Statement" and "StatementNoShortIf" kind of statements as its body. Definition of "Statement" is given as:
StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement
As you can notice, it does not include LocalVariableDeclarationStatement.

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

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by OCAJO1 »

Ok. So as for proper programming, use of brackets alleviates the need for remembering (or worrying about) such rules. But as for the exam, is this rule something that needs memorizing? Thanks

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

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by admin »

No, not important for the exam.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by enthunoob »

Printing the names of all the Employees in a list of Employees, computing interest for a list of Accounts, or something as simple as computing the average of a list of numbers, require you do deal with a list of objects instead of one single object.
Spelling mistake: to

Is it worth reporting these here?

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

Re: [HD Pg 352, Sec. 12.4.1 - arraylist-and-collections]

Post by admin »

>Is it worth reporting these here?
Yes, definitely. All errors reported here will be fixed in the next build.
We sincerely appreciate your feedback.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 80 guests