Accessing Array Elements


Java array elements are accessed using indices within brackets. For example, to retrieve the value of an element of a 1-D array at a given index, you would use this general syntax ”

 value = array_name[index]; 

Keep in mind that Java array indices start from 0. Similarly, to set the value of an element of a 1-D array at a given index, you would use this general syntax ”

 array_name[index] = value; 

Multidimensional array elements are accessed in the same manner using indices for each dimension of the array. For instance, to access the value of an element of a 2-D array, you would type

 value = array_name[index1][index2]; 

Example: Accessing Array Elements

Let's modify the Air class once again by adding a new variable that represents the molar mass of the gas mixture. The molar mass of a gas mixture is the sum of the mole fraction of each species multiplied by the species molar mass. In the Air3 class, the value of the molarMass variable is computed in the constructor by accessing the appropriate elements of the moleFr[] and species[] arrays.

The Air3 class also includes methods to return the mole fraction value or Species object at a specified index in the array.

 public class Air3 {   private double pressure, temperature, molarMass;   private Species species[] = new Species[2];   private double moleFr[] = { 0.7885, 0.2115 };   //  The value for the molarMass variable is computed   //  inside the constructor by accessing the elements   //  in the moleFr[] and species[] arrays.   public Air3(double p, double t) {     pressure = p;     temperature = t;     species[0] = new Species("N2",0.0280134);     species[1] = new Species("O2",0.0319988);     for(int i=0; i<species.length; ++i) {       molarMass +=               moleFr[i]*species[i].getMolarMass();     }   }   //  The element of the species[] array at index i   //  is obtained using the indexer operator, []   public Species getSpecies(int i) {     return species[i];   }   //  The value of the moleFr[] array at index i   //  is obtained using the indexer operator, []   public double getMoleFraction(int i) {     return moleFr[i];   }   public double getPressure() {     return pressure;   }   public double getTemperature() {     return temperature;   }   public double getMolarMass() {     return molarMass;   }   } 

Just to be sure everything works, here is a simple driver program that creates an Air3 object and retrieves the value of the molarMass variable.

 public class AccessDemo {   public static void main(String args[]) {     Air3 gas = new Air3(101325.0, 300.0);     System.out.println("molar mass is " +                         gas.getMolarMass());   } } 

Output ”

 molar mass is 0.0288563121 

For an example of accessing the elements of a 2-D array, see "A 2-D Numerical Grid" example earlier in this chapter.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net