File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes iterate through float values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "iterate through float values" Watch "iterate through float values" New topic
Author

iterate through float values

Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190

Hi

I am doing a sticky note program for myself and I have a JPopupMenu with a hide command in each note. I want to take advantage of the new translucency
in Java 6 Update N. The translucency method takes a float value so I need to iterate from 0.9f to 0.0f in a loop. I did it like so with an ArrayList of the values except I think this is a dumb way of doing it. The code runs smoothly and it give that exact desired effect. Is there any way to iterate through float values 9 to 0 without first adding those values to an ArrayList?

Thanks

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Hi,

why not just use a for loop like this:

Marco
Isaac Hewitt
Ranch Hand

Joined: Jul 24, 2006
Posts: 190

Thanks so much Marco.

Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

No problem

Unfortunately I don't know of a more elegant solution. Some Basic dialects had for loops where you could give a step width for this.

Perhaps you could refactor this code to a separate method which generates the sequence of values. But then you'd have to use an iterator or something with another loop. Don't know which one is better. Anyway I hope it's working...

Marco
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
What's the problem with a normal loop?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1278

Oops, point for you, Irina :roll:

I don't know why the brain sometimes tries to find ideas that are more complicated than...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

Originally posted by Irina Goble:
What's the problem with a normal loop?
The problem with that loop is that the values of f that it produces are these:

0.9
0.79999995
0.6999999
0.5999999
0.4999999
0.39999992
0.29999992
0.19999993
0.09999993

So it runs one time fewer than what you expected.
Irina Goble
Ranch Hand

Joined: May 09, 2004
Posts: 91
Originally posted by Paul Clapham:
So it runs one time fewer than what you expected.


Thank you, Paul. Tricky.
Using integers surely better, better precision and simple.
Atleast doubles will give all 10 steps, but then they have to be converted to float and 1.3877788E-16 is not quite a 0.0.

gives:
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
1.3877788E-16
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iterate through float values
 
Similar Threads
HashMap<String, ArrayList>
ArrayList to Byte Array
Initializing Arraylists inside a loop
Pixel Transparency
ArrayList test not working?