![]() |
![]() |
|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Today we'll experiment together in the Code Pad. We introduce arrays -- like lists, they are containers which can hold other objects; unlike lists, they are of a fixed size (they don't shrink or grow). Unfortunately, they also come with all their own new syntax for getting, setting, declaring, and constructing1.
String[] names; // Declare a variable, of type array-of-String
names = new String[4]; // Initialize the variable.
names[0] = "North"; // Initialize the contents of the array;
names[1] = "South"; // this is kinda like the LinkedList method
names[2] = "East"; // add(String,int) except that it *replaces*
names[3] = "West"; // any String previously stored at that index
// (whereas lists shift all existing elements over by one).
names[2] // Calling the getter for the field [2] -- kinda like a list's "get" method.
names.length // Like a list's "size()" method (but really we're accessing a field).
// A for-each loop works equally well with both lists *and* arrays.
String allNames = "";
for ( String s : names ) { // In Code Pad: Shift-Enter, to continue a line
allNames = allNames + ", " + s;
}
allNames
String allNames2 = "";
for ( int i=0; i < names.length; ++i ) {
allNames2 = allNames2 + ", " + names[i]; // Calling the getter for each field.
}
allNames2
|
double[] logTable;
logTable = new double[100];
logTable // In Code Pad, double-click on the red object to inspect it.
// Call the setter for the field 17:
logTable[17] = 3; // Look in the inspection window, to see the change.
for (int i = 0; i < logTable.length; ++i ) {
logTable[i] = Math.log10(i); // Call setter for the field [i]
}
// See how the inspection window has changed.
import java.awt.Color;
int SHADES = 20; // How many purples to generate.
Color[] purples = new Color[SHADES];
for (int i=0; i < purples.length; ++i ) {
int shade = (int) (256*((double)i/(double)SHADES)); // 256*(i/SHADES)
purples[i] = new Color(shade,0,shade);
}
purples
purples[5].getRed()
purples[5].toString()
|
String s = "This is a sentence with several words. And, two spaces between sentences.";
s.split(" ") // Words separated by a single space.
s.split(",") // Words separated by a comma.
s.split(" +") // Words separated by one-or-more spaces2
|
Why add all this new syntax, when Java could have just made arrays a class, and then had normal methods for getting and setting and constructing? Because Java wanted to look like some older languages, as a marketing ploy to get people to use it.
For what it's worth, Their marketing ploy worked (Java was adopted by many older programmers), at the expense of having different-syntax-for-similar-concepts.
On the plus side, the array notation is more concise than the full method calls, in a way which is effective at communicating info. It mirrors the subscript notation used by mathematicians for hundereds of years.
↩2 The character + is a special pattern-maker; it means one-or-more-of-the-preceding pattern. Other handy patterns:
// "[0-9]" any one single digit.
// "[0-9]+" one or more digits
// "[0-9]+.[0-9]+" A double: one or more digits, followed by '.', followed by one or more digits.
// Words separated by one or more punctuation characters: "[ ,.!?;]+".
"Hi, who is it? You?!? Noooo!".split("[ ,.!?;]+")
|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
| ©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland |
![]() |