Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Possible to group different ints to do one command?

Thread view: 
rckildea@hotmail.com - 19 Aug 2007 16:29 GMT
I'm writing a code, and to save space I'm wondering how I could do
this. There's no easy way to do this so I'll just give an example:

int blueCoins = playerBlueCoins;
int redCoins = playerRedCoins;
int greenCoins = playerGreenCoins;
//...and so on...

Is there some way for me to say "If the value of any of these ints is
greater than 99, the value is 99" without having to write it for every
single int?
Lew - 19 Aug 2007 16:43 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?

Build it up pairwise with Math.min().

If you're looking for a comparison amongst all the ints, you are perforce
going to examine every int.  There is not a way to circumvent physics.

Signature

Lew

Lew - 19 Aug 2007 16:48 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?

Ultimately there is no way to find the min() of a set of comparable values
without examining every value.

You could build it up pairwise with Math.min() or use
java.util.Collections.min() over a Collection of the values (including
Integer.valueOf(99) ).

Signature

Lew

Thomas Hawtin - 19 Aug 2007 16:55 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:

Don't worry so much about space as complexity and understandability.

> int blueCoins = playerBlueCoins;
> int redCoins = playerRedCoins;
[quoted text clipped - 4 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?

Well, you could write a class to represent a number of coins in this
context. Otherwise you are left with:

        int  blueCoins = Math.min(playerBlueCoins , 99);
        ...

Tom Hawtin
Patricia Shanahan - 19 Aug 2007 16:58 GMT
> I'm writing a code, and to save space I'm wondering how I could do
> this. There's no easy way to do this so I'll just give an example:
[quoted text clipped - 7 lines]
> greater than 99, the value is 99" without having to write it for every
> single int?

Any time you find yourself saying something like "for every single int"
reconsider whether you should have an array or map that could be
processed in a for-loop, rather than several separate variables.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.