Re: infinite recursion
On Thu, 11 Oct 2007 10:30:23 -0700, andreyvul <andrey.vul@gmail.com>
wrote, quoted or indirectly quoted someone who said :
short[] allOptions(short val) {
Vector<Short> cc = new Vector<Short>(0);
Iterator<Short> it;
short[] rv;
//check which numbers are available
short i;
for (i = 0; i < 9; ++i)
//bitmask check (1 == unavailable, 0 == available)
if (((1 << i) & val) == 0)
//available, add number to cc
cc.add(cc.size(), (short)(i + 1));
//create return array
rv = new short[cc.size()];
it = cc.iterator();
i = 0;
while (it.hasNext())
rv[i++] = it.next();
return rv;
}
Here is your code tidied up a bit:
Vector -> ArrayList
use of for:each loop
one parm add
standard idiom for i
don't reuse i.
import java.util.ArrayList;
public class Temp
{
short[] allOptions( short val )
{
ArrayList<Short> cc = new ArrayList<Short>(10);
// Check which numbers are available
for ( int i = 0; i < 9; i++ )
{
// Bitmask check (1 == unavailable, 0 == available)
if ( ( ( 1 << i ) & val ) == 0 )
{
// Available, add number to tail end of cc
cc.add( (short)( i + 1 ) );
}
}
// Create return array using for:each loop
// We can't use toArray since it
// will not accept short[] as a parm
short[] rv = new short[ cc.size() ];
int i = 0;
for ( short s: cc )
{
rv[i++] = s;
}
return rv;
}
}
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com