116

I have this code

public static Boolean freq[] = new Boolean[Global.iParameter[2]];
freq[Global.iParameter[2]] = false;

could someone tell me what exactly i'm doing wrong here and how would i correct it? I just need to initialize all the array elements to Boolean false. thank you

7 Answers 7

315

I just need to initialize all the array elements to Boolean false.

Either use boolean[] instead so that all values defaults to false:

boolean[] array = new boolean[size];

Or use Arrays#fill() to fill the entire array with Boolean.FALSE:

Boolean[] array = new Boolean[size];
Arrays.fill(array, Boolean.FALSE);

Also note that the array index is zero based. The freq[Global.iParameter[2]] = false; line as you've there would cause ArrayIndexOutOfBoundsException. To learn more about arrays in Java, consult this basic Oracle tutorial.

Sign up to request clarification or add additional context in comments.

3 Comments

The second piece of code is beneficial for the case when we wanna initialize it (set it) to all true values. Great answer!
I'd rather inverse the variable name and use the default initialization with false values.
I am actually in favor of Boolean[] array as it allows you to have three states for each element: true, false and null, which is useful for cases where you want to note that something is "not processed".
22

The array will be initialized to false when you allocate it.

All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.

4 Comments

He's using Boolean, not boolean, so it will default to null.
Correct. I did not see that. Arrays.fill should do the trick.
Does this apply to all versions of Java?
@Spyre: Yes. This behavior is part of the definition of the language.
9

Arrays in Java start indexing at 0. So in your example you are referring to an element that is outside the array by one.

It should probably be something like freq[Global.iParameter[2]-1]=false;

You would need to loop through the array to initialize all of it, this line only initializes the last element.

Actually, I'm pretty sure that false is default for booleans in Java, so you might not need to initialize at all.

Best Regards

1 Comment

He's using Boolean, not boolean, so it will default to null.
5

They will be initialized to false by default. In Java arrays are created on heap and every element of the array is given a default value depending on its type. For boolean data type the default value is false.

2 Comments

He's using Boolean, not boolean, so it will default to null.
i meant to use boolean, my bad...sorry for the mislead
2

public static Boolean freq[] = new Boolean[Global.iParameter[2]];

Global.iParameter[2]:

It should be const value

1 Comment

this is not true. could be variables as well. the problem is that you are using Global.iParameter[2] to access the array. but the indices are going from 0 until only Global.iParameter[2]-1 !!
0

The main difference is that Boolean is an object and boolean is an primitive.

  • Object default value is null;
  • boolean default value is false;

Comments

0

We can also use List collection to store boolean data:-

List<boolean>result=new List<>();
result.add(condition);
return result;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.