- Forward


Using 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
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 multiple 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]; age = new int[3];

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

The Original Example Revisited
Back SMYC Forward
int[] income; income = new int[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;
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)
There's Always More to Learn
Back -