Array Length Determined when the Program Runs
Because an array object is constructed as the program runs,
its size can determined at run time.
Here is the previous example, with some modifications:
import java.io.* ;
class inputArray
{
public static void main ( String[] args ) throws IOException
{
BufferedReader inData = new BufferedReader ( new InputStreamReader( System.in ) );
int[] array;
// determine the array size and construct the array
System.out.println( "What length is the array?" );
int size = Integer.parseInt( inData.readLine() );
array = new int[ _____________ ];
// input the data
for ( int index=0; index < array.length; index++)
{
System.out.println( "enter an integer: " );
array[ index ] = Integer.parseInt( inData.readLine() );
}
// write out the data
for ( int index=0; index < array.length; index++ )
{
System.out.println( "array[ " + index + " ] = " + array[ index ] );
}
}
}
|
|