Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Array ISYS 350.

Similar presentations


Presentation on theme: "Java Array ISYS 350."— Presentation transcript:

1 Java Array ISYS 350

2 Array An array allows you to store a group of items of the same type together. Processing a large number of items in an array is easier than processing a large number of items stored in separate variables.

3 Declaring a Array Examples of declaring an array:
int[] anArray = new int[10]; 10 elements index from 0 to 9 double[] anArrayOfDoubles = new double[10]; String[] anArrayOfStrings = new String[10]; int[] anArray = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

4 Array Elements Array elements are indexed from 0 to array size – 1.
Each element can be accessed by its index: arrayName[index] Ex: empName[0] intRate[2]

5 Array Initialization With the declaration statement:
String[] empName = { "Peter", "Paul", "Mary" }; double[] intRate = { .03, .04, .05, .06, .07, .08 }; Initialize each element separately: empName[0] = "Peter"; empName[1] = "Paul"; empName[2] = "Mary";

6 Accessing Array Elements with a for loop
Using array’s length property: for (arrayIndex = 0; arrayIndex <= empName.length-1; ++arrayIndex) { out.print(empName[arrayIndex]); } Note: length - 1

7 Example: Compute the sum and average of numbers in an array
<% String[] stName = { "Peter", "Paul", "Mary", "Nancy", "Linda"}; double[] myGPAs = { 2.5, 3.2, 3.4, 2.9, 3.6 }; for (int arrayIndex = 0; arrayIndex <= stName.length-1; ++arrayIndex) { out.print(stName[arrayIndex] + ": GPA= " + myGPAs[arrayIndex]); out.print("<br>"); } double sumGPA=0, avgGPA; for (int i = 0; i <= myGPAs.length - 1; ++i) sumGPA += myGPAs[i]; avgGPA = sumGPA / myGPAs.length; out.print("Average GPA is: " + avgGPA); %>


Download ppt "Java Array ISYS 350."

Similar presentations


Ads by Google