0
  for (int i=1;i<10;i++)
  {
        if something == true
            do something
        else 
            do something
  }

Here is what I want to do: Outside the loop For, I need to summarize that at which i something = true and at which i something = false.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
John
  • 3,888
  • 11
  • 46
  • 84
  • Can you clarify a little? I'm pretty confused. – Mike Feb 09 '11 at 23:01
  • If I understand your question, possibly [something like this](http://stackoverflow.com/questions/2082449/how-to-filter-an-array-in-java) is what you're looking for – Ismail Badawi Feb 09 '11 at 23:03
  • what do you want to put in an array and how does "i" relate to that something being true? – CoolBeans Feb 09 '11 at 23:03
  • I think he wants to iterate an array and know the index of the elements that meet a certain condition. Péter Solution does just that. – jhurtado Feb 09 '11 at 23:06

4 Answers4

6
List<Integer> positiveResults = new ArrayList<Integer>();
List<Integer> negativeResults = new ArrayList<Integer>();

for (int i = 1; i < 10; i++)
{
    if (someCondition)
        positiveResults.add(i);
    else
        negativeResults.add(i);
}

where someCondition is supposed to be a boolean variable or expression.

If you explicitly want the results in an array instead of a List, then add

Integer[] resultsInArray = positiveResults.toArray(
        new Integer[positiveResults.size()]);
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • @Shakedown, indeed :-) I had some slight feeling that something is not right with that line, but it didn't overcome my consciousness threshold. Thanks! – Péter Török Feb 09 '11 at 23:09
  • 1
    Deleting my answer, as yours has evolved to be a better version of the same approach. (Though it is worth pointing out that `something == true` is the same as `something`, and it's generally considered best practice to say (for example) `users[i].isActive()` rather than `users[i].isActive() == true`.) – StriplingWarrior Feb 09 '11 at 23:15
  • @Péter Török: How can I print out positiveResults using System.out.println? – John Feb 09 '11 at 23:25
  • @Kalla, simply `System.out.println(positiveResults)`. – Péter Török Feb 09 '11 at 23:31
  • @StriplingWarrior, +1 and a badge for fair play :-) I edited my answer just while you were editing your comment. – Péter Török Feb 09 '11 at 23:37
  • @Péter Török:it prints out an array such as [1,2,3]. I want it print out 1, 2,3. How can I do it? Thanks! – John Feb 09 '11 at 23:38
  • @Shakedown: `if(something == true), right?` - I hope not. Writing `x==true` instead of `x` is just an obfuscation. You get the same boolean you started with. If you like it, why don't you use `(x==true) == true`? It must be even better, mustn't it? – maaartinus Feb 09 '11 at 23:39
  • @maaartinus, the original code in the post (upon which he commented) was `something = true`. – Péter Török Feb 09 '11 at 23:42
  • @Peter: This is one more reason to point out that writing this makes no sense. With `something` being `Boolean` (object not primitive) it even leads to wrong results. – maaartinus Feb 09 '11 at 23:50
  • @maaartinus, IMHO it was a step in the right direction, even if it didn't solve *all* the problems with that code snippet at once. – Péter Török Feb 10 '11 at 00:01
  • @maaartinus: I agree - I'd rather simply write `if(something)` but I wasn't going to mention it as it's a moot point in this discussion. – Nate W. Feb 10 '11 at 00:22
  • OK. I always prefer to clear such things. It's not important, but a too much code gets plagued by such things. – maaartinus Feb 10 '11 at 01:28
1

You seem to have your for loop set up wrong, the conditional statement should be in the middle:

for (int i = 1; i < 10; i++)

but to answer the question, you just need to declare the variable before the loop:

int i = 1;
for ( ; i < 10; i++) {

}
// You can still access i here since it is still in scope.
DaveJohnston
  • 10,031
  • 10
  • 54
  • 83
  • Sorry, I might have been too quick to answer the question and not consider why you would actually want to do it. This answer just shows you how to declare the variable and still access it outside the loop. The other answers will probably be better in terms of what you should really be doing in your case. – DaveJohnston Feb 09 '11 at 23:07
0

First, your loop conditions are in the wrong order for what you probably want:

for (int i = 1; i < 10; ++i)

Next, I guess you want

if (something == true)
{
    // do something
}
else
{
    // do something *else*
}

in the loop body.

Anyway, in the do something part, you have the value of i since it's locally scoped. At that point you can add it to an array of your choosing.

John
  • 15,990
  • 10
  • 70
  • 110
0

Example with List:

List<Integer> trueStates = new ArrayList<Integer>();

    for (int i=1;i<10;i++)
    {
        if (something == true) //e.g (i%2 == 0)
            trueStates.add(i);      
    }

After for-loop you will have in trueState i which something == true. Another option is use Map which hold you state of all yours numbers.

Example with Map:

Map<Integer, Boolean> statesMap = new HashMap<Integer, Boolean>();
for (int i=1;i<10;i++)
{
    statesMap.put(i, something == true);        
}
lukastymo
  • 26,145
  • 14
  • 53
  • 66