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 / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

Test If Int Has Not Been Set

Thread view: 
dimo414 - 26 Oct 2006 06:00 GMT
I have an array of integers, which I set via a recursive function.
Obviously, I don't want to set them more than once, so I want to test
if, for instance myVar[10] == null.  But of course, that doesn't work,
since integer's are not null before they've been given a value.

How can I test if a value has been set or not?
Andrew Thompson - 26 Oct 2006 06:15 GMT
...
> How can I test if a value has been set or not?

<sscce>
class NullInteger {
 public static void main(String[] args) {
   Integer[] allInt = new Integer[10];
   for (int ii=0; ii< allInt.length; ii++) {
     System.out.println(
       "index " + ii + " \tvalue: " + allInt[ii] );
   }
   for (int ii=0; ii< allInt.length; ii+=3) {
     allInt[ii] = new Integer(ii+1);
   }
   for (int ii=0; ii< allInt.length; ii++) {
     System.out.println(
       "index " + ii + " \tvalue: " + (allInt[ii]==null) );
   }
 }
}
</sscce>

HTH

Andrew T.
Patricia Shanahan - 26 Oct 2006 06:35 GMT
> I have an array of integers, which I set via a recursive function.
> Obviously, I don't want to set them more than once, so I want to test
> if, for instance myVar[10] == null.  But of course, that doesn't work,
> since integer's are not null before they've been given a value.
>
> How can I test if a value has been set or not?

1. If the integers have an out-of-bound value, initialize them to that
first. For example, if the values you set are all non-negative,
initialize to -1. If they must be positive, just check for myVar[10]==0.

2. Use Integer and check for null, As Andrew suggested.

3. Use a separate boolean array, the same length, and set the boolean at
an index to true when you set the int with the same index.

4. Structure your logic so that you only attempt to set each element once.

Patricia
Thomas Weidenfeller - 26 Oct 2006 09:27 GMT
> 3. Use a separate boolean array, the same length, and set the boolean at
> an index to true when you set the int with the same index.

Or use a bit set to the same effect.

> 4. Structure your logic so that you only attempt to set each element once.

5. Don't use an integer array - assuming your algorithm produces a
sparse vector. If this is the case consider a map with the index
becoming the key.

/Thomas
Signature

The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq



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.