AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Test If Int Has Not Been Set

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
dimo414
Guest





PostPosted: Thu Oct 26, 2006 7:11 am    Post subject: Test If Int Has Not Been Set Reply with quote



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?
Back to top
Andrew Thompson
Guest





PostPosted: Thu Oct 26, 2006 7:11 am    Post subject: Re: Test If Int Has Not Been Set Reply with quote



dimo414 wrote:
....
Quote:
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.
Back to top
Patricia Shanahan
Guest





PostPosted: Thu Oct 26, 2006 7:11 am    Post subject: Re: Test If Int Has Not Been Set Reply with quote



dimo414 wrote:
Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.