1

I'm trying to change this code to a for loop, but i have some problems

panel[1].setBackground(Color.red);
            panel[2].setBackground(Color.white);
            panel[3].setBackground(Color.red);
            panel[4].setBackground(Color.white);
            panel[5].setBackground(Color.red);
            panel[6].setBackground(Color.white);
            panel[7].setBackground(Color.red);
            panel[8].setBackground(Color.white);
            panel[9].setBackground(Color.red);
            panel[10].setBackground(Color.white);

new code - for

for (int i = 0; i < panel.length; i++) {
                panel[(i*2)+1].setBackground(Color.red);//i think that is correct, or no?
                panel[(i*3)+1].setBackground(Color.white); //problem here
            }

thanks

1
  • 7
    Consider the values of (i*2)+1 and (i*3)+1 as your loop progresses. One the first iteration, i = 0, so they will be (0*2)+1 = 1 and (0*3)+1 = 1, so we're already off to a bad start. Commented Feb 23, 2011 at 21:52

5 Answers 5

8

Use a new-style for loop:

int ct = 0;
for(JPanel panel : panels){
   panel.setBackground((ct % 2 == 1) ? Color.Red : Color.White);
   ct++;
}
Sign up to request clarification or add additional context in comments.

13 Comments

+1 I would rather have a reference for current color than modulo operator on a int var though. Looks a bit cleaner that way stackoverflow.com/questions/5097601/problem-with-loop-for-java/…
Agree with @Oscar. There's little point using an iterator if you have to maintain an index/count independently anyway.
@Fel what does that mean? a) There is an item, but you don't want to change it's color b) you are leaving the zero index of your array empty?
@Fel: you do have an [0] you are just not putting anything in it, it is still there and it would cause this answer to fail with a NullPointerException
@Sean, you never do any library code, do you? you do have bounds checking and absolutely type safety... Also if you remove bit ops, java is dead on the servers, no protocols for you
|
3

Solution

for (int i = 1; i < panel.length; i++)
{
    if ( i % 2 == 0 ) { panel[i].setBackground(Color.white); }
    else { panel[i].setBackground(Color.red); }   
}

Or a more concise expression using the ternary operator:

for (int i = 1; i < panel.length; i++)
{
     panel[i].setBackground( i % 2 == 0 ? Color.white : Color.red );  
}

Explaination

% is the modulo operator, i % 2 == 0 when i is even, != 0 when odd.

Caveats

Your array of panels referencing in your example starts at 1, arrays in Java start at ZERO, you might have a potential one off error here if you have anything in the (first) ZERO array element.

Using the type safe List classes is always better than working with arrays directly, you would not have to deal with the one off error problems you are creating by not using the first array slot.

4 Comments

whats teh point of using if else conditions when you can increment an array with 2?
that would not work with a list of odd items, read what the % operator is for.
i % 2 == 0 ? panel[i].setBackground(Color.white) : panel[i].setBackground(Color.red); } this will not compile in java
@bestsss: fixed it, copy and pasted the wrong thing originally
3
for(int i = 1; i<panel.length; i++)
{
    if(i%2 == 0)
    {
        panel[i].setBackground(Color.white);
    }
    else
    {
        panel[i].setBackground(Color.red);
    }
}

Comments

3

I would:

Color current = Color.white; 
for( Panel p : panels ) { 
   p.setBackground( current );
   current =  ( current == Color.white ? Color.red : Color.white );
}

2 Comments

hi! in the 2nd example you have red/white, in the 1st white/red. i.e. need to start w/ red (before the loop)
Removed 2nd example as only makes noise
-1
for (int i = 1; i < length; i+=2)
{
    panel[i].setBackground(red);
    panel[i+1].setBackground(white);
}

1 Comment

this will cause an IndexOutOfBoundsException as well as not work odd numbers of items

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.