Calculating the number 8 chars long which contains all possible numbers containing the numbers from 0 to n I have the following code:

    Integer cr = 3;
    String y = "\"\r\n\"";

    for (Integer i = 0; i &lt; cr; i++)
    {
        for (Integer j = 0; j &lt; cr; j++)
        {
            for (Integer k = 0; k &lt; cr; k++)
            {
                for (Integer l = 0; l &lt; cr; l++)
                {
                    for (Integer m = 0; m &lt; cr; m++)
                    {
                        for (Integer n = 0; n &lt; cr; n++)
                        {
                            for (Integer o = 0; o &lt; cr; o++)
                            {
                                for (Integer p = 0; p &lt; cr; p++)
                                {
                                    consolus.append(i.toString()+j.toString()+k.toString()+l.toString()+m.toString()+n.toString()+o.toString()+p.toString() + y);
                                }
                            }
                        }
                    }
                }
            }
        }
    }


Is there some way I can write this more efficiently? Essentially, the output is a number 8 chars long which contains all possible numbers containing the numbers from 0 to cr. This method currently works, however it doesn't seem efficient, and then writing to the TextView consolus, only occurs after all the for statements complete.
Presumably you want to avoid the 8 levels of nested loops?  (This isn't an efficiency thing, merely a code cleanliness issue.)

If so, use a length-8 array of integers, and use it to do "counting" in a while loop.

int[] cnt = new int[8];

while (1) {

    // ... Do something ...

    // Update
    for (int i = 0; i &lt; 8; i++) {
        cnt[i]++;
        if (cnt[i] != cr) break;
        cnt[i] = 0;
    }
}


I'll leave termination of the while loop as an exercise for the reader...

Note: If you find this pattern occurring all over the place in your code, you could be cunning and put the update logic into a helper class.  And then your outer loop could become:

for (Counter c = new Counter(8,cr); c.isActive(); c.increment()) {
    ...
}