I've annotated the source. I don't know if will be helpful at all (Sane seems to have covered it fine), but it couldn't hurt.
public class BarChart {
public static void main( String[] args ) {
int array[] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
System.out.println( "Grade distribution: " );
// For every element in the array...
for ( int counter = 0; counter < array.length; counter++ ) {
if ( counter == 10 )
System.out.printf( "%5d: ", 100 );
else
System.out.printf( "%02d-%02d: ", counter * 10, counter * 10 + 9 );
// A helper variable to make things easier to understand.
int gradeFreq = array[counter];
// print a star "gradeFreq" times
for ( int i = 0; i < gradeFreq; i++ )
System.out.print( "*" );
// Print a new line to seperate the bars
System.out.println();
}
}
}
Alternatively, depending on the version of Java you have installed, you can iterate through an array like this:
public class Foreach {
public static void main( String[] args ) {
int[] array = { 1, 2, 3, 4, 5 };
// Print out every element of the array
for ( int element : array ) {
System.out.println( element );
}
}
}
EDIT: In reply to your second question, given:
int array[] = { 1, 1, 1 };
Doing this:
Will result in an array that looks like this: