[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Back to Parameterized Classes. On to Vectors (1).
Held: Monday, 24 October 2005
Summary: Today we visit one of Java's core data structures: the array. We also consider applications of arrays in writing algorithms.
Related Pages:
Assignments
Notes:
Overview:
Vec2D classes allowed
you to group logical aspects of a two-dimensional vector into a whole.
type[] name = new type[size];
stuff,
you would write
double[] stuff = new double[5];
names, you would write
String[] names = new String[10];
null for objects).
type[] name = { val1, val2, ..., valn }
;
int[] vals = {3, 2, 6, 5};
String[] rebelguys = { "Samuel", "William", "Jonathan", "Daniel" };
name[index]
stuff with
stuff[3]
pen.println(rebelguys[0] + " is a rebel."); stuff[3] = stuff[3] * 2;
i held the value 3, then we could get
element 3 of the stuff with
stuff[i]
name.length
/**
* Sum the values in stuff.
*/
public double sum(double[] stuff) {
double total = 0.0; // The total of the numbers in stuff
int i; // A counter variable for stepping through
// the array
// Step through the array, adding each subsequent value
for(i = 0; i < stuff.length; i = i + 1) {
total = total + stuff[i];
}
// Return the total value
return total;
} // sum
public long fib(int n) {
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
} // fib(int)
public long fibra(int n)
{
// Build a cache, use -1 for "I don't know."
int cache = new long[n+1];
cache[0] = 0;
cache[1] = 1;
for (int i = 2; i <= n; i++)
cache[i] = -1;
// Compute the nth number using the cache.
fibra_helper(n, cache);
} // fibra(int)
public long fibra_helper(int n, long[] cache)
{
// If the cache contains no value
if (cache[n] == -1)
// Compute it
cache[n] = fibra_helper(n-1, cache)
+ fibra_helper(n-2, cache);
// The cache must now return a value, so
// return it.
return cache[n];
} // fibra_helper
Back to Parameterized Classes. On to Vectors (1).
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 6 09:47:33 2005.
The source to the document was last modified on Thu Aug 25 16:15:08 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Outlines/outline.30.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby