7.2.2 Creating Arrays
Unlike declarations for primitive data type variables, the declaration of an array varial not allocate any space in memory for the array. It creates only a storage location for th ence to an array. If a variable does not contain a reference to an array, the value of the v is null. You cannot assign elements to an array unless it has already been created. A array variable is declared, you can create an array by using the new operator and assi reference to the variable with the following syntax:
arrayRefVar = new element Type [arraySize];
This statement does two things: (1) it creates an array using new element Type [arraySi and (2) it assigns the reference of the newly created array to the variable arrayRefVar.
– of the arraj
– arable can be combirted in one statement as
elementType [] arrayRefVar = new elementType[arraySize];
or
elementType arrayRefVar[] = new elementType [arraySize];
double [] myList = new double [10] ;
This statement declares an array variable, myList, creates an array of 10 elements of double type, and assigns its reference to myList. To assign values to the elements, use the syntax
arrayRefVar [index] = value;
For example, the following code initializes the array:
This array is illustrated in Figure 7.1.
myList [0] = 5.6;
my List [1]= 4.5;
myList [2]= 3.3;
myList [3]= 13.2;
myList [4]= 4.0;
myList [5]= 34.33;
myList [6]= 34.0;
myList [7]= 45.45;
my List [8]=99.993;
myList [9]= 11123;
GIPHY App Key not set. Please check settings