Sample Code from "Java How to Program, Fifth Edition" from Deitel and De
itel.
There are three ways of declaring a simple array:
- int myarray[];
myarray=new int[10];
- int myarray[]=new int [10];
- int myarray[]={1, 22, 58, 66, 82, 88, 66, 2, 5, 0};
note that arrays are objects, therefore need to use keyword "new"
Cool thing about arrays: they have a data member "length" which knows
the length of the array
The following code shows the initialization of an array.
InitArray.java
- Try the three methods of initialization listed above
- Try using a constant instead of 10: "final int ARRAY_LENGTH = 10;"
An application of using arrays to calculate the statistics of a die
which has been rolled 6000 times.
RollDie.java
- Note: Math.random() produces a number between 0 and 1 (excluding 1).
- The "(int)" truncates off the decimal part (yielding a number between 0 and 5), thus 1 is added
- The 0 element is not used to simplify coding
Pass by Value or Pass by Reference
General rule:
- objects are passed by reference
- primitives (int, char, double, long, boolean, etc) are passed by value
Code (Applet) that demonstrates passing by reference and pass by value
PassArray.java
PassArray.html
Dealing with Multi Dimensional Arrays
Can have different number of columns for each row
Two ways of creating Multi Dimensional Arrays
- Initialization method:
int myarray[][]={{1,2,3},{4,5},{6}};
- Declaring (without initialization)
int myarray[][];
int myarray= new int[3][]; //how many rows
int myarray[0]=new int[3]; //how many columns in first row
int myarray[1]=new int[2]; //how many columns in second row
int myarray[2]=new int[1]; //how many columns in third row
Multi.java
Notice how the print function is able to determine how many
rows using: "array.length" and how many columns in each row using
"array[row].length"
- Exercise: Currently, code to create and initialize array1
through the second method (mentioned above) is commented out in
Multi.java.
Modify the code so that both array1 and array2 are created
and initialized through method 2.
Strings
First, we should discuss the basis of strings: chars. Why are chars
16 bits instead of 8 bits like in C/C++? Because of Unicode.
Unicode expands ascii and has the capability of handling characters
from different languages.
For more on Unicode go to: www.unicode.org
Some sample code showing Unicode characters (Let me know if it works on your machines--I'm having John look at fixing Unicode on the Sun Rays)
Unicode.java
Code showing the multiple String Constructors:
StringConstructors
The constructors with 3 arguments have the breakdown:
- arg1: array name
- arg2: starting index of array
- arg3: number of characters to copy
Bonus: not shown in study group. Demonstrates the following functions:
- s1.length() // Note: need the "()" for strings as compared to arrays.
- s1.charAt(count) //returns the character at index "count"
- s1.getChars(0, 5, charArray, 0)
- arg1: starting index in s1
- arg2: index that is one past the last character to be copied in s1
- arg3: character array into which the characters are to be copied
- arg4: starting index of "charArray" (for copying)
StringMiscellaneous.java
Comparing Strings through the following functions/operators:
- s1.equals("hello") //lexicographical comparison
- s1=="hello" //compares addresses
- s3.equalsIgnoreCase(s4) //lexicographical comparison (ignoring case)
- s1.compareTo(s2) // lexicographical comparison. Returns negative if s1 is less than s2,
returns positive if s1 is greater than s2, and
returns 0 if s1 and s2 are equal
- s3.regionMatches(0, s4, 0, 5)
- arg1: starting index in s3
- arg2: comparison string
- arg3: starting index in s4
- arg4: number of characters to compare between the two strings
- s3.regionMatches(true, 0, s4, 0, 5) // same as above except "true" indicates that case will be ignored
StringCompare.java
String is the only object that you don't have to use the keyword
"new" with.
For instance, you can say:
String mystring="hello";
Exercise: explore the difference between
String mystring="hello"; and
String mystring= new String("hello");
- See what happens to the "==" when you use: String s1="hello"
- See what happens when you create String s5="hello"
compare it to s1 with "==".
For further exploration on this, I suggest you use the following code with
Object Tool (downloadable from the online course)
StringCompare2.java
Breakdown:
- save the code to the "Object Tool" directory
- compile it in that directory with "javac StringCompare2.java"
- run object tool with "java OT" (note case sensitive)
- In the "Code Entry" box of the ObjectTool Interpeter type: "StringCompare2 myob=new StringCompare2();"
- Notice that an object is created under the Local Variables section.
- You can click on "myobj" and see it displayed in the "Display Controller"
- What do you notice about s1 and s5?