COP2800 : Java Programming Chapter 5

Arrays and Strings

Section A Arrays

1) Array: is a list of data items that all have the same type and the same name. You declare an array variable in the same way as you declare any scalar variable. But you insert a pair of square brackets after the type.
Example : double[] salesFigure;
After you create an array variable, you still need to create the actual array.
You use the same procedure to create an array that you use to create an object.
(1) double[] salesFigure; // declare variable
(2) salesFigure = new double[20]; // reserves memory for 20 salesFigures objects.
To declare variable and reserve memory in 1 step:
double[] salesFigure== new double[20];
(page 200)

2) Subscript: is an integer contained within square brackets that indicates 1 on an array's variables, or elements. (page 200)

3) Reference: Array names actually represent computer memory addresses; that is, array names are references, as are all Java objects.

4) null: When you declare an array name, no computer memory address is assigned to it.
Instead, the array variable name has the special value null.
When you declare int[] someNums;,
the variable someNums has a value of null....
but the array using new: int someNums = new int[10];,
then someNums has an actual memory address value and each element= zero because it is a numeric array. (page 202)

5) Initializing array: one way is tenMult[]={10,20,30,40,50,60};
you do not give the array size because the size will be defined by the number of values given.
(page 205)
Another way is to use a for loop to go thru each element of the array to set to a value.
Example: for ( x=0; x < 7; ++x) // x is the subscript or the element
emp[x]= new Employee(101+x, 5.35); // x is empNum element, the second x adds x to the value .
To print the values for each, you can write the following
for x=0; x < 7; ++x)
System.out.println(emp[x].getEmpNum()+" " +emp[x].getSalary());
// object[].method();

6) Constructors for arrays: You must explicitly call individual constructors for array objects.

7) Passing Arrays to Methods: someMethod(someArray[x]);
You pass a single array element to a method in exactly the same manner you would pass a variable. The array element is passed by value; that is, a copy is made (no permit change).
You can pass an array to a method. Arrays, like all objects, are passed by reference; the method has access to the actual values in the array elements (valued used and can change).
(Page 216)

8) length field: contains the number of elements in the array.
Example: double[] salaries = new double[8];
salaries.lenght is assigned the value 8.
(page 220)

Back to the Top

Section B Strings

1) Literal string: a sequence of characters enclosed within double quotation marks.

2) new: Keyword used to with String constructor to create a String object, just like arrays.

3) Reference: a String variable name is a reference, just like an array.

4) Immutable: String objects cannot be changed so they are immutable.

Back to the Top

Links to COP2800 Java Programming Chapters:
Links to the java.sun.com to help with Java Programming

Back to the Top