开发者

How do I check if a variable has been initialized

First of all, I'm fairly new to Java, so sorry if this question is utterly simple.

The thing is: I have a String[] s made by splitting a String in which every item is a number. I want to cast the items of s into a int[] n.

s[0] contains the number of items that n will hold, effectively s.length-1. I'm trying to do this using a foreach loop:

int[] n;
for(String num: s){
    //if(n is not initialized){
        n = new int[(int) num];
        continue;
    }
    n[n.length] = (int) num;
}

Now, I realize that I could use something like this:

int[] n = new int[(int) s[0]];
for(int i=1; i < s.l开发者_运维技巧ength; i++){
    n[i-1] = (int) s[i];
}

But I'm sure that I will be faced with that "if n is not initialized initialize it" problem in the future.


You can't cast a String to an int. Java is strongly typed, and there is no implicit type conversion like you might find in a scripting language.

To convert a String to an int, use an explicit conversion, like Integer.parseInt(String).

All member variables and elements of arrays are initialized with a default value. For int types, the value is 0. For reference types (any subtype of Object), the default value is null. Local variables don't get a default value, but the compiler analyzes the code to ensure that a value is assigned before the variable is read. If not, the code will not compile.

I think what you want is something like this:

int[] n = new int[Integer.parseInt(s[0]);
for (int idx = 0; idx < n; ++idx)
  n[idx] = Integer.parseInt(s[idx + 1]);


You can't check if a variable is initialized in your code, because by definition reading from a variable that might not have been initialized leads to a compile-time error.

You can initialize the variable to null and check for that value if your variable is not a primitive type and null is not a valid value after the initialization.

In the specific example the second code you showed would definitely be cleaner.


int[] n = null;
for(String num: s){
    if(n == null){
        n = new int[(int) num];
        continue;
    }
    n[n.length] = (int) num;
}


If you initialize your variables or objects to null prior to using them,

String myVar1 = null;
Person bobby = null;

You could comparing the variable or object to not null,

if (myVar != null) {
  // myVar has a value.
}


You might want to take a look at the other datastructures in the collections framework.

-> http://java.sun.com/developer/onlineTraining/collections/Collection.html

ArrayList is a reasonable good alternative to arrays


Only thing you can do is check against null. If your code is within a method, it will not compile if you don't initialize. So if it compiles and run you know it is initialized to at least null and then do the null checking.


As others have noted, the closest thing to a "right way" to do this is to initialize the array to null.

On other points:

"n[n.length]" will throw an "index out of bounds" exception. Arrays have elements ranging from 0 to length-1. In any case, I think what you intended to say in the first case was "n[0]" and in the second was "n[i]".

Storing the size of an array in the first element is probably a bad idea. It can be done for an int array, but it would be messy in a String array and it wouldn't work at all for a boolean array. Even in the int case, you're now mixing two very different things in the same data structure, which is likely to be confusing. If the array size is fixed, "length" holds the size anyway. If the array size is variable and you're thinking that you're going to create an array big enough and then store the amount you actually use, you are better off using ArrayList, which handles dynamic-size arrays cleanly.


I ran into this issue, checking if an int has been initialized. In my program, it was possible that it was initialized to 0, so checking if (int i = 0) wouldn't do much good.

As a solution/work-around, I created a boolean variable that is set when the int is initialized. Hope this helps.


You can check for null:

int[] n;
for(String num: s){
    if(n == null) {
        n = new int[(int) num];
    }
    n[n.length] = (int) num;
}

Note that this can only happen if n is a class member. If it is a local variable, the compiler will not let you do anything with it without it being initialized.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜