• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enhanced For that Does Indeed Initialize an Array - see code

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the question?
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:What's the question?



Bear,

Thank-you for chiming in. I read that it is not possible for an enhanced for loop to initialize an array, but this code proves that it is possible. Does this not count though since I had to use brackets in the enhanced for?

Why would an enhanced for not be able to initialize an array?

Thank-you for replying Bear. That is awesome you had the opportunity and knowledge to write a javascript book.

Respectfully,

Ted
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you read that? Of course it's nonsense. Perhaps the author meant something else?
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear,

Thank-you for confirming that it is possible to initialize an array in an enhanced for loop.

I read this in OCA Java SE 7 Programmer 1 Certification Guide by Gupta. I did help proofread this book and it is excellent for the most part except for some small errors here or there (nothing is perfect). This is like software though where there is always going to be a mistake per so many lines of code, even in stuff written by NASA.

On page 282 it says, "The enhanced for loop can't be used to initialize an array and modify its elements"



Maybe the author did mean something else. It is a book worth buying and reading to learn how to program in java. I am only testing this statement because it is possible and I am trying to make sure I am understanding this stuff.

Thank-you again for confirming that it is possible to initialize an array in an enhanced for loop. I hope this does not break Gupta's feelings.

Regards,

Ted
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion the author has some point when she states it is impossible to initialize an array and/or modify its elements when using nothing but an enhanced for loop.

Take a look at the following code snippet.


The output:
aaa
bbb
ccc
aaa
bbb
ccc


This clearly proves the author's statement: using just an enhanced for loop it's impossible to modify its elements. But of course it's possible to create a counter variable and use it in combination with the enhanced for loop to modify the elements of an array (like you have shown in your example). But then the regular for loop would be more appropriate. I think that particular statement (on page 282) should be rephrased better to avoid confusion. Maybe something like: "Using only the enhanced for loop it's impossible to initialize an array or modify its elements".
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

Thank-you for the interesting feedback. I see what you mean. I guess if only the enhanced for is used then changing or initializing the array or ArrayList is impossible. Thank-you for illustrating your point with code. I agree, the statement should be worded a little more clearly and maybe provide some more code examples. The book is great though; I like reading it and I am still learning from it.

Check out this code that modifies the contents of an array using an enhanced for with a switch construct:



This does not invalidate your point though since a switch block is used instead of only a for each loop.

Respectfully,

Ted
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ted,

Mala Gupta's book is at this moment one of the better books (and even maybe the best) to prepare for the OCAJP7. And it's a new kid (book) on the block, so it's written from scratch. Other books are mostly updates from a previous version, so they have already survived more (technical) reviews. That's probably why some statements could/should be rephrased a bit to avoid confusion or make things more clear.

I must say I like the way you prepare for the exam: creating lots of different code snippets and writing little programs to give you lots of code practice. That's really a must-do when preparing for the exam. The 2 code examples you posted in this thread are great to prepare you for the exam and get a good grasp on things. But please don't write these enhanced for loops when you work as a java programmer/developer. It's not clean, easy, readible code But for learning purposes (almost) everything is allowed.

(ps. package names should always be lower case according to conventions, but as you already noticed it won't give a compiler error if you don't)

Regards,
Roel
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel,

Thank-you for the positive feedback, insightful information, and helpful tips on code conventions. It is hard to find useful information so I appreciate the feedback.

Also, it is my understanding that a switch construct can make more efficient byte code than a if-if-else-then block - at least I read something along these lines at Enthuware.

Regards,

Tedster
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code conventions can be found here. And it's the 1st hit when you google on code conventions java, so I would not say it's hard to find

From my experiences using a switch statement is bad OOP style. And I definitely don't want to oversimplify things: sometimes you really need a switch statement and there is no other alternative. But sometimes a switch statement can (and should) be replaced by something more OO (like polymorphism). You should (always) think twice when using a switch statement as it may be an indication of a bad (OO) design.
 
reply
    Bookmark Topic Watch Topic
  • New Topic