• 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

Array prints out 0's?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so let's say I am having a user input scores and at the end I want to print out the results so I do something like:



The user only inputs lets say 5 scores out of possible 1000.
I then try to print it out by doing something like this:



how would I go about printing out only the index's that were input, because right now it prints out the scores and then 995 0's after.

Thanks in advance for any advice!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly does your code look like to input the user's scores?

You could keep a counter there to keep track of how many scores were actually entered, and then let your loop go up to the value of the counter, instead of to Scores.length.
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:What exactly does your code look like to input the user's scores?

You could keep a counter there to keep track of how many scores were actually entered, and then let your loop go up to the value of the counter, instead of to Scores.length.



Oh, that makes a ton of sense haha I don't know why I didn't think of that, thanks Jesper I appreciate it!
 
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And one more thing.

Use post increment counter++ not pre-increment ++counter.
In this case, there's no difference, but still, logically suppose to be post increment.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Use post increment counter++ not pre-increment ++counter.
In this case, there's no difference, but still, logically suppose to be post increment.


Actually, I dispute that.

Personally, I prefer to use pre-increment everywhere, because you're far less likely to get unexpected results. And since it makes absolutely no difference whatsoever in a for loop, I'd say: use whatever seems best.

That said, post-increment in for loops IS more usual - but why, I'm not exactly sure...

Winston
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote: but still, logically suppose to be post increment.


I too must refute that post increment is "logically" what you're supposed to do. Rather, post increment is by far the most common form used with the for-loop so it becomes the de facto standard convention. When used elsewhere and when the order of operation matters, which one is logically correct depends entirely on the requirements.

I don't know why post increment ended up being the usual convention used with the for-loop either. I know it's been like that since the K&R C Programming book days so tradition may have as much to do with it more than anything.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I know it's been like that since the K&R C Programming book days so tradition may have as much to do with it more than anything.


I wonder if there was an efficiency aspect to it? I can certainly understand that, in a multi-threaded situation, "read then increment" might be a bit more robust, since the value isn't dependant on an update op; but since the 808x hardware K&R were working with was single-threaded (I think), even that wouldn't appear to be a concern.

But hey, there's nothing wrong with tradition.

Winston
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion there is a simple logic clash between "for" loop specification and preincrement operation.
They just contradicts to each other.

Still I would prefer to use post increment, as in most literature and university's course you would find it as default in for loops.

But I also agree with a guys, that there's no strong proven explanation, why should be used one operation instead of other.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:In my opinion there is a simple logic clash between "for" loop specification and preincrement operation.
They just contradicts to each other.


Sorry, but I just don't see there being anything logically significant about it. To quote from one my favorite movies: "You keep using that word. I don't think it means what you think it means."
 
Liutauras Vilda
Marshal
Posts: 8863
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Outside the loops you understand the different of preincrement and postincrement and it makes sense.
In for loop, there's no difference which one to use, you still get the same final result.
The for loop works in specific order when perform operations, that's why there's no difference in post and pre incrementation.

Don't know how to explain more clear, sorry about that
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah... I think I see it now.

"The increment expression is invoked after each iteration through the loop" (emphasis mine) versus pre-increment. Seems like I've met my match in you for being picky about the smallest things
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Junilu. It is simply a tradition.

Maybe K&R used i++ to emphasise that the increment operation follows the execution of the loop.
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guy's, as for my counter increment, my book for Java Programming in college told us that it was more efficient and showed us it's running time difference between i++ & ++i the book told us to use ++i to be more efficient even though the time saved is very minimal but yeah just my saying in why I used preincrement I'm still in this java class so I could be wrong buts its what I've learned
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are the two pages from my book in case you were curious!
Screenshot_2014-11-17-07-35-58.png
[Thumbnail for Screenshot_2014-11-17-07-35-58.png]
Screenshot_2014-11-17-07-36-14.png
[Thumbnail for Screenshot_2014-11-17-07-36-14.png]
 
Michaell Sam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and I mean more efficient in loops at least
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have heard people say that ++i is slightly faster than i++, but you can see from those book pages that the difference is slight. I would suggest you might have an old book if it uses the millisecond method rather than this.
The advice to put the less likely operand before the && operator depends on, “all other things being equal.” If you for example you follow that and write an equals method like this:-
… ob.getClass()equals(this.getClass(()) && ob != null …
the null test is much likelier to be true than getClass(), but getClass is the one which will throw the exception, so the null test must precede the getClass test. Most people don't use that null test however.

Please beware of posting such screenshots; depending where you live they might be regarded as violation of copyright.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...you will encounter programmers who insist on using either postfix or prefix increments in their loops. You should follow the conventions established by your organization.


So I'm not alone then. The advice is good though. Follow the established convention.

As you become an experienced programmer, you will discover other ways to enhance the operation of the programs that you write. You should always be on the lookout for ways to improve program performance.


In some environments, especially at many startups where Time to Market is the key consideration, this will probably be true and it will probably be encouraged by management.

However, also remember what Donald Knuth said: "Premature optimization is the root of all evil."

Do not optimized based on your gut feeling or intuition. Programmers are notoriously bad at doing that. Use a profiler to see where the performance bottlenecks in your code.

On the other hand, if you have your eye on the long term, the above quote can be modified to:

As you become an experienced programmer, you will discover ways to refactor the programs that you write. You should always be on the lookout for ways to improve program design, clarity, and maintainability.



This is the kind of mindset that Agile teams have and in general, successful teams that produce great software.

 
reply
    Bookmark Topic Watch Topic
  • New Topic