The Artima Developer Community
Sponsored Link

Java Answers Forum
Thinking in Java --> Arrays

2 replies on 1 page. Most recent reply: Mar 14, 2008 8:25 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
john camara

Posts: 1
Nickname: dbgotd
Registered: Feb, 2008

Thinking in Java --> Arrays Posted: Feb 25, 2008 4:48 PM
Reply to this message Reply
Advertisement
A question for anyone who has read "Thinking in Java" by Bruce Eckel:

On page 197 (in the 4th edition), he has a class called "ArrayInit". In this class, he creates two Integer arrays to demonstrate the different ways of instantiating an array. I found it interesting that arrays can be initialized with a final comma in the list of initializers. He mentions as an exaplanation:
"The final comma in the list of initializers is optional. (This feature makes for easier maintenance of long lists.)"

It's never used in the book again (unless I missed it) and I've never seen it used anywhere else in code/java tutorials/etc. It obviously works, and I've used it since. My question is, is this actually useful in any way? Can anyone give an example? And also, does anyone know _why_ this feature would make for easier maintenance of long lists?

Thanks!

-JP


George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Thinking in Java --> Arrays Posted: Feb 26, 2008 5:41 AM
Reply to this message Reply
I find they make editing easier when working with lists where I keep each item on a separate line, e.g.:
        String[] names = {
            "joe",
            "bob",
            "bill",
        };

If I want to transpose the names "bob" and "bill", I just have to cut the entire bob line and paste it below the bill line. No messing with removing the comma from bob and adding it to bill.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Thinking in Java --> Arrays Posted: Mar 14, 2008 8:25 AM
Reply to this message Reply
I do not think it helps a programmer in any way by making his coding effort easier. I also do not think that it was an intensional feature added by the designer for any reason. Sometimes you get some features that are by-production of the way something is implemented. I am of the opinion that it does help tools that generate code. For example, if a tool is reading some text from a file and trying to build the array initialization then it can just keep grabing the values, add a comma at the end and then push it to the list. If the comma at the end is not allowed then tool has to check if it s the first item in the list or subsequent (it is your logic how you do it, but the truth is that you need some condiitonal logic here) and then add the comma or not. Except that I cannot think of allowing a comma at the end of list helps a programming in any way.


Thanks
Kishori

Flat View: This topic has 2 replies on 1 page
Topic: Thinking in Java --> Arrays Previous Topic   Next Topic Topic: Arrays and loops

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use