I am trying to form a string ( of length 10,000 ) with 1's and 0's based on a certain condition 'valid'.
Although I am able to do this, I am concerned about the performance and would need some help to decide on which of the below two methods would be better performant.
Method 1 (Using Stringbuilder)
StringBuilder output = new StringBuilder();
for( int i = 0; i < 10000; i++ )
{
if ( valid )
{
output.append( "1" );
}
else
{
output.append( "0" );
}
}
Method 2 (Using integer array)
int[] gridArray = new int[ 10000 ];
for( int i = 0; i < 10000; i++ )
{
if ( valid )
{
gridArray[i] = 1;
}
}
//Convert grid array to string output
Also, how best to convert the integer array to a String?
BitSetmake it more efficient to form a string with1s and0s? It still usesStringBuilderintoString().