Sie sind auf Seite 1von 14

Lecture 5 Arrays

A way to organize data


MIT AITI 2003

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.

Declaring an Array

Array declarations use square brackets. datatype[] label


For example: int[] prices String[] names

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 }

Conceptual Overview of an Array

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

Accessing Arrays cont

To access an item in an array, type the name of the array followed by the items index enclosed in square brackets. For example, the expression: names[0] may return the String: Robert

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 Hello Hello Hello Hello Hello Conor. Dan. Evita. Sonia. Robert. Jehanzeb.

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.

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.

Alternative Way to Construct Arrays

Another way to make an array is to specify all of the items at its creation. To do this, use curly brackets to surround the array and separate items with commas. Heres 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.

Alternative Way to Construct Arrays

Yet another way of creating our Array:


String[] names[0] names[1] names[2] names[3] names[4] names[5] names = new String[6]; = Conor; = Dan; = Evita; = Sonia; = Robert; = Jehanzeb;

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).

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

Das könnte Ihnen auch gefallen