- Forward


Arrays
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • Variable:
    • A named space for holding data/information
    • The name is often referred to as the identifier
  • Literal:
    • A representation of a datum
  • Assignment:
    • Placing a value into the memory identified by a variable/constant
Opinions?
Back SMYC Forward
double income1, income2, income3, income4, income5, income6, income7; income1 = 550.75; income2 = 25.60; income3 = 347.10; income4 = 120.45; income5 = 885.00; income6 = 10.99; income7 = 123.45;
Motivation
Back SMYC Forward
  • An Observation:
    • Situations like the one above, in which we have collections of similar information, are very common
  • Naming Such Collections:
    • We'd like to be able to give the items similar names
    • Naming schemes like the one above are "messy" and not very flexible
An Example from Algebra
Back SMYC Forward
  • Awful:
    • \(t = a + b + c + d + e\)
  • Messy:
    • \(t = x\text{1} + x\text{2} + x\text{3} + x\text{4} + x\text{5}\)
  • Better:
    • \(t = x_1 + x_2 + x_3 + x_4 + x_5\)
  • Why is it Better?
    • It is possible to refer to a generic element as \(x_i\)(where \(i\) is also a variable)
    • It makes it clear that the different variables are related
    • It makes it possible to use short-hand notation like: \(t = \sum_{i=1}^{n} x_i\)
An Array in Memory
Back SMYC Forward

An array is a contiguous block of memory that can be used to hold a fixed number of homogeneous elements.

images/array.gif
Using Arrays in Java
Back SMYC Forward
  • Declaration using the [] Modifier:
    • Syntax: base_type[] array_name;
    • Example: int[] income;
  • Accessing Elements using the [] Operator:
    • Syntax: array_name[index]
    • Example: income[5] = 250000;
    • Example: total = income[3];
  • A Reminder About Indexes:
    • 0-based (i.e., the "first" element has an index of 0)
When You Know All of the Elements "Up Front"
Back SMYC Forward

One can declare an array and assign all of the elements to it using an "array literal" (formally, an array initializer) as follows:

char[] course = {'C','S','1','3','9'}; int[] age = {18, 21, 65};

One can then assign elements or access elements using the [] operator.

When You Don't Know All of the Elements "Up Front"
Back SMYC Forward

One can declare an array and assign default values to all of the elements to it as follows:

char[] course; int[] age; course = new char[5]; \\ Each element is initialized to '\0' age = new int[3]; \\ Each element is initialized to 0

One can then assign elements or access elements using the [] operator.

The Original Example Revisited
Back SMYC Forward
double[] income; double = new double[7]; income[0] = 550.75; income[1] = 25.60; income[2] = 347.10; income[3] = 120.45; income[4] = 885.00; income[5] = 10.99; income[6] = 123.45; int day; double bad; day = 2; bad = income[day];
Arrays of Objects
Back SMYC Forward
// Declare the array Color[] usFlag; // Construct/instantiate the array (i.e., allocate memory for 3 references) usFlag = new Color[3]; // Construct/instantiate each of the elements of the array usFlag[0] = new Color(204, 0, 0); // Red usFlag[1] = new Color(255, 255, 255); // White usFlag[2] = new Color( 0, 0, 204); // Blue
An Observation about Invalid Elements
Back SMYC Forward
  • Using the "Messy" Approach:
    • The error will be detected at compile-time because the name won't have been declared
  • Using Arrays:
    • The error won't be detected until run-time (i.e., when the index that is out of bounds is used an ArrayIndexOutOfBounds "error" will be generated)
Elements of Arrays as Parameters
Back SMYC Forward
  • Actual Parameters:
    • Syntax: array_name[index]
    • Example: x = payment(income[0]);
  • Formal Parameters:
    • Can't be array elements
Arrays as Parameters
Back SMYC Forward
  • Formal Parameters:
    • Syntax: (base_type[] array_name)
    • Example: payment(int[] income)
    • Example: main(String[] args)
  • Actual Parameters:
    • Syntax: (array_name)
    • Example: y = payment(taxableIncome);
Returning an Array
Back SMYC Forward
  • Syntax:
    • base_type[] method_name([formal_parameter]...)
  • Example:
    • public int[] schedule(){...}
Attributes and Methods Owned by Arrays
Back SMYC Forward
  • Attributes:
    • length contains the number of elements (and is initialized when memory is allocated)
  • Methods:
    • clone() makes a (shallow) copy
An Example of the length Attribute
Back SMYC Forward
int n; int[] strikeouts; strikeouts = new int[9]; // This will work n = strikeouts.length; // This will not work because the length attribute // is declared to be final (which means it cannot be // changed after it is initialized) // strikeouts.length = 5;
Java vs. Python - Important Differences
Back SMYC Forward
  • The Functionality:
    • In Python, a List is used to provide this kind of functionality
  • "Literals":
    • In Python, List literals are written using [ and ] (not { and })
  • Length:
    • In Python, the length of a List can be obtained using the len() function
Java vs. Python - Important Differences (cont)
Back SMYC Forward
  • Special Indexes:
    • In Python, the "index" -1 can be used to work with the last element of the List
    • In Python, a range of indexes can be specified by including a : in the "index" (e.g., data[2:5])
  • Presence:
    • In Python, the in operator can be used to determine if a List contains a particular element
  • Size:
    • In Python, the number of elements in a List can be changed using the append(), insert(), extend(), and remove() methods
There's Always More to Learn
Back -