go to previous page   go to home page   go to next page highlighting

Answer:

strArray[1] = "World" ;    

Length of an Array

Recall these facts about all arrays:

  1. Each element of an array is of the same type.
    • In the example, each element is of type "reference to String".
  2. The length of an array (the number of cells) is fixed when the array is created.
    • In the example, the array is length 8.

Frequent Bug: It is easy to confuse the length of an array with the number of cells that contain a reference. In the example, only two cells contain references to Strings, but strArray.length is 8.

Abundant Source of Hopeless Confusion: Often people say "an array of Strings" or "a String in the array" when really they should say "an array of String references" or "a String referenced by a cell of the array". This can lead to confusion. Be careful when you encounter such phrases.

Here is code that declares and constructs the array in one statement, and then puts some String references into it:

// combined statement
String[] strArray = new String[8] ;  

strArray[0] = "Hello" ;
strArray[1] = "World" ;
strArray[2] = "Greetings" ;
strArray[3] = "Jupiter" ;
Praying Mantis

QUESTION 4:

Must all the Strings in the array be of the same length?

Remember, this really is asking "must all of the Strings pointed to by cells of the array be of the same length?".


go to previous page   go to home page   go to next page