Don Lester
Don Lester

Reputation: 37

How do I modify this code to make every even number index in an array true?

Here's what I have:

  boolean[] even = new boolean[array.length];
  for (int i = 0; i < array.length; i++) {
      even[i] = (array[i] % 2) == 0;
  }

I want indexes (0,2,4,6,8,etc) to be true, and indexes (1,3,5,7,9,etc) to be false

I think this code is more accurate to what I'm trying to do, but it won't compile.

  for(int i = 0; i < array.length ; i++) {
      int val = (array[i] % 2);
      if(val == 0)
          array[i] = true;
      else
          array[i] = false;
  }

Upvotes: 0

Views: 283

Answers (7)

SoulRayder
SoulRayder

Reputation: 5166

You code can be modified to the following:

 for(int i = 0; i < array.length ; i++) {

  if(array[i]%2==0)
      even[i] = true; //assign to even , not array
  else
      even[i] = false; //assign to even, not array
  }

to get what you want.

Upvotes: 0

Jon Newmuis
Jon Newmuis

Reputation: 26502

In the first example, you're writing to the array even, which is an array of booleans.

In the second example, you're writing to the array array, which (while not shown) must be an array of integers, as that's where you're reading the integers from.

Try:

for(int i = 0; i < array.length ; i++)
{
    int val = (array[i] % 2);
    if(val == 0)
        even[i] = true;
    else
        even[i] = false;
}

Upvotes: 0

xxlali
xxlali

Reputation: 1036

try this;

for(int i = 0 ; i<array.length ; i++){
    if(i % 2 == 0){
        array[i] = true;
    }
    else{
        array[i] = false;
    }
}

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

Instead of performing a modulus and a branch which are both expensive operations you can use some loop unrolling and optimise the loop.

boolean[] even = new boolean[array.length];
for (int i = 0; i < array.length; i += 2) {
    even[i] = true;
    // not needed as boolean[] are set to all false by default
    // even[i+1] = false;
}

BTW a boolean[] uses one byte per value on most platforms. A more efficient approach might be to use a BitSet.

BitSet even = new BitSet(array.length);
for (int i = 0; i < array.length - 1; i += 2)
     even.set(i);

This will use as little as 1/8th of the memory.

Upvotes: 0

Kajetan Abt
Kajetan Abt

Reputation: 1545

Your mistake is really small:

You are not checking whether your index is divisible by two, but rather, if the value in the array is divisible by 2.

boolean[] even = new boolean[array.length];
for(int i = 0; i < even.length ; i++)
{
if(i % 2 == 0)
    even[i] = true;
else
    even[i] = false;
}

Edit: Apparently everyone takes about the same length of time to answer, and now we have seventy identical answers.

Upvotes: 0

Steve Chambers
Steve Chambers

Reputation: 39424

In the first snippet, change:

  even[i] = (array[i] % 2) == 0;

to:

  even[i] = (i % 2) == 0;

In the second snippet, change:

int val = (array[i] % 2);

to:

int val = i % 2;

Upvotes: 1

Adarsh
Adarsh

Reputation: 3641

You need to use even[i] = true instead of array[i] = true. I am assuming array is an array of integers and even is an array of boolean values. So, you need to assign the boolean value to the index of a boolean array.

Upvotes: 0

Related Questions