No. Sometimes strArray[j]
is null, so strArray[j].length()
will not work.
Here is the complete program:
private class StringArray { public static void main ( String[] args ) { String[] strArray = new String[8] ; strArray[0] = "Hello" ; strArray[1] = "World" ; strArray[2] = "Greetings" ; strArray[3] = "Jupiter" ; strArray[ strArray.length-1 ] = "the end" ; for (int j=0; j < strArray.length; j++ ) if ( strArray[j] != null ) System.out.println( "cell " + j + ": " + strArray[j] ); else System.out.println( "cell " + j + ": " + "empty" ); } }
You may wish to copy this program to your editor and run it. First-string programmers will run this code and see:
cell 0: Hello cell 1: World cell 2: Greetings cell 3: Jupiter cell 4: empty cell 5: empty cell 6: empty cell 7: the end
What will the following statement do? Assume that it immediately follows the last statement of the program:
strArray[0] = "Good-by" ;