- Forward


Understanding the Use of Arrays
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review: Opinions?
Back SMYC Forward

What do you think of the following fragment?

total = 0.0; total = total + income1; total = total + income2; total = total + income3; total = total + income4; total = total + income5; total = total + income6; total = total + income7; mean = total / 7.0;
Review: An Example of an Array
Back SMYC Forward

Arrays offer the same kinds of benefits of subscripts in mathematical notation -- this fragment is more compact, more readable, and more flexible.

total = 0.0; for (int i=0; i<n; i++) { total = total + income[i]; } mean = total / income.length;
Review: An Array in Memory
Back SMYC Forward
array
Review: A Physical Analogue
Back SMYC Forward
  • The Entities:
    • An element of an array is like an apartment
    • An array is like an apartment building
  • The Physics:
    • Apartments can't be moved, created, or destroyed
    • Each apartment can be referred to by number
    • Can change the contents of each apartment but not the apartments themselves
Review: Arrays in Java
Back SMYC Forward
  • Declaration using the [] Modifier:
    • Syntax: base_type[] array_name;
    • Example: int[] income;
  • Memory Allocation using the [] Length Specifier:
    • Syntax: array_name = new base_type[length];
    • Example: income = new int[20];
  • Accessing Elements using the [] Operator:
    • Syntax: array_name[index]
    • Example: income[5] = 250000;
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 = meanPayment(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 i; int[] strikeouts; strikeouts = new int[9]; for (i=0; i<strikeouts.length; i++) { strikeouts[i] = 0; }
An Example of the length Attribute (cont.)
Back SMYC Forward
int i; int[] strikeouts; strikeouts = new int[9]; . . . // 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;
An Example of the clone() Method
Back SMYC Forward
int[] copy, original; original = new int[3]; original[0] = 2001; original[1] = 1995; original[2] = 2001; copy = original.clone();
Common Faults and Symptoms
Back SMYC Forward
  • Common Faults:
    • Failing to allocate any memory
    • Failing to allocate enough memory
    • Accessing element 0
    • Accessing element length
  • Obvious Symptoms:
    • NullPointerException when using method/attribute
    • IndexOutOfBoundsException when accessing an element
  • Less Obvious Symptoms:
    • Calculations that omit the 0th or last element (or both)
Arrays Utility Class
Back SMYC Forward

The Arrays class (in the java.util package) class contains several static methods that are very convenient when working with arrays.

import java.util.Arrays; int target; int[] a; a = new int[100]; Arrays.fill(a, 0); // Fill a with zeroes Arrays.fill(a, 1, 10, 99) // Fill a[10] thru a[99] with ones ... Arrays.sort(a); // Sort a in ascending order // Searches for 5 in a and returns 5 if found // The array must be sorted // target = Arrays.binarySearch(a,5);
There's Always More to Learn
Back -