0

How can you fill a boolean vector with numbers in every spot instead of false/true?

    public static void initateTakenWords(boolean[] takenWords)
{
    for(int i = 0; i<takenWords.length; i++)
    {
        takenWords [i] = i;
    }
}

this is my code in one of my methods, but i cant initialize numbers to the takenWords vector.

  • 1
    You can't. That's why it's a `boolean` array and not an `int` array. – resueman Oct 16 '15 at 12:53
  • Why would you do that? Obviously you can't do that... Why is the array `boolean` in the first place and not `int`? – dguay Oct 16 '15 at 12:53
  • Although your given code doesn't contains `vector`, don't use it. [Here's why](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) – sam Oct 16 '15 at 12:56

1 Answers1

0

You cannot do that. If you want to initialize your array to numbers instead of FALSE and TRUE then you need to make your array as Integer array rather than Boolean.

On a side note:

Instead of using a Loop to initialize your array to a default value you can use the Arrays#fill()

boolean[] arr = new boolean[size];
Arrays.fill(arr, Boolean.FALSE);
resueman
  • 10,572
  • 10
  • 31
  • 45
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331