TYPE arrayName[ constantSize ]; |
Example:
int a[100]; // array of 100 integers double b[10]; // array of 10 doubles |
Compare the difference with Java:
int[] a = new int[100]; // array of 100 integers double[] b = new double[10]; // array of 10 doubles |
int a[10]; Storage structure in C++: |
|
Furthermore:
|
An array in Java is stored indirectly:
int[] a; // defines variable a a = new int [10]; // record location of array in a Storage structure in Java: |
Example:
Example: Find smallest element in an array
int a[10]; int min, i; min = a[0]; for ( i = 0; i < 10; i++ ) { if ( a[i] < min ) min = a[i]; } cout << "min = " << min << endl; |
|
When you access an array element a[i], the index i may be out of bound
Java inserts code in the program to perform runtime array bound checks and an out of bound array index will cause a run time error
|
C++ does not perform runtime array bound checks !!!