Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5 Arrays A way to organize data © MIT AITI 2003.

Similar presentations


Presentation on theme: "Lecture 5 Arrays A way to organize data © MIT AITI 2003."— Presentation transcript:

1 Lecture 5 Arrays A way to organize data © MIT AITI 2003

2 What are Arrays Conceptually, Arrays are a series of compartments to hold data with each compartment sized appropriately for whatever data type the array as a whole is intended to hold. An array can holds only one type of data! E.g. only int, char, String, etc.

3 Declaring an Array Array declarations use square brackets. datatype[] label For example: int[] prices String[] names

4 Creating a new Array Here is the syntax: new int[20] The new keyword creates an array of the type int that has 20 compartments Combined with assignment: int[] prices = new int[20] When created as above, the array has the default “zero” in each of the compartments i.e. The items in the array are initialized to the zero-value for the particular type; { int:0, float:0.0, String: null }

5 Conceptual Overview of an Array

6 Accessing Arrays Every compartment in an array is assigned an integer value starting at zero. This number is called the index of an item In Java and most other languages, the index starts from 0 and ends at n-1, where n is the size of the array

7 Accessing Arrays cont… To access an item in an array, type the name of the array followed by the item’s index enclosed in square brackets. For example, the expression: names[0] may return the String: “Robert”

8 Example 1 String[] names = {“Conor”, “Dan”, “Evita”, “Sonia”, “Robert”, “Jehanzeb” }; }; for (int i = 0; i < names.length; i++) { System.out.println(“Hello “ + names[i] + “.“); } Which prints: Hello Conor. Hello Dan. Hello Evita. Hello Sonia. Hello Robert. Hello Jehanzeb.

9 Modifying Array Elements First, get the particular item as shown in the previous slide. Then, set that expression equal to (assignment) the new value. names[0] = “Sonia” Now this item has been changed from one name to another. So the expression, names[0] returns “Sonia”. Important: The size of an array cannot be changed after it is created as items in the array can only be changed but not added.

10 Example 2 int[] fibs = new int[10]; fibs[0] = 1; fibs[1] = 1; for(int i=2; i<fibs.length; i++) { fibs[i] = fibs[i-2]+fibs[i-1]; } After running this code, the array fibs contains the first ten Fibonacci numbers.

11 Alternative Way to Construct Arrays Another way to make an array is to specify all of the items at it’s creation. To do this, use curly brackets to surround the array and separate items with commas. Here’s an example of creating an array of names: String[] names = { “Conor”, “Dan”, “Evita”, “Sonia”, “Robert”, “Jehanzeb”}; This creates an array with six compartments containing the specified items. Note that all the items must be of the same type. Here they are all strings.

12 Alternative Way to Construct Arrays  Yet another way of creating our Array: String[] names = new String[6]; names[0] = “Conor”; names[1] = “Dan”; names[2] = “Evita”; names[3] = “Sonia”; names[4] = “Robert”; names[5] = “Jehanzeb”;

13 2-Dimensional Arrays There are instances where it is useful to work with arrays that relate two indices to a single value. e.g. The temperature (value) on each date (index2) at several locations (index 1) in Ghana. This is accomplished with 2-Dimensional Arrays; An Array of an Arrays. In Java this is done using two sets of square brackets. For example: double[][] temps = new double[10][365]; Here there are 10 location arrays with 365 compartments each (1 for each day).

14 Accessing 2-D Array Elements To access all the temperatures from one location use temps[index1]. The above expression returns an array of doubles i.e. double[] To get the temperature on date (index 2) at a particular location use temps[index1][index2] The above expression returns a double


Download ppt "Lecture 5 Arrays A way to organize data © MIT AITI 2003."

Similar presentations


Ads by Google