Lecture 16 Notes

advertisement
BIT 115
Arrays
Page 1 / 3
Arrays (Conceptual Overview)
 Multiple, Identical elements
(any type can be used – all must be the same type)
 Immediately adjacent in memory
 Inititialized
(Draw the box picture on the board)
Make sure to number the boxes.
Why would you want to use them?
Up till now, everything you've seen can be solved in a predetermined amount of space.
Larger programs don't have this property
How many letters will you type into a Word doc? How could Word ever possibly know
this?
Need to be able to change the amount of space at run-time
You can only declare variables at design-time
You can change the length of the array at run-time
Design-time: When you're writing the code
Run-Time : As the program is running
Since you tell Java which space in the array you want using a number, and you know how
to increase a number, you can get to the new space you created.
Declaring An Array
int [] arrayOfInts = new int[5];
Important Points:
1. You can use any type. For now, stick with int or double
2. Variable is named like normal
3. [] are REQUIRED
4. Elements are initialized to default value – for integers, this is 0
Using an array
arrayOfInts[1] = 2;
int copyOfElementOne;
copyOfElementOne = arrayOfInts[1];
System.out.println("Copy is: " + copyOfElementOne); //prints Copy is 2
System.out.println("Slot is: " + arrayOfInts[1]); //prints Slot is 2
System.out.println("Slot is: " + arrayOfInts[0]); //prints Slot is 0
Important Points:
1. []are REQUIRED
2. subscript (in this case, 1) is used to indicate which element
3. Index starts at 0
So an array with 2 elements is indexed 0, 1
This is known as a "zero indexed array"
4. Same syntax for left or right side of assignment statement
5. If you ask for an element that isn't there, you'll get a "Subscript Out Of Range" error
BIT 115
BIT 115
Page 1 / 3
BIT 115
Arrays
Page 2 / 3
Subscripts, Expressions, and Loops
Point out that the RHS of the assignment to an array element can be a constant (1), a variable (i), or an
expression (i *2)
Explain that the subscript of an array can be an expression as well.
Loops
Whenever you hear the phrase "do something a bunch of times", you should think "loop".
Loops lets you repeat an action a bunch of times
Array lets you store a bunch of values
Hey – these go well together!
ICE: Worksheet
ICE: Simple Array Stuff
Passing An Array To A Method (service)
2 parts:
1. Declaring the parameter
2. Passing the array to the method (service)
Step 1:
In the parameter list, put the [] after the type, similar to declaring one as a variable
Step 2:
Make sure NOT to include () after the name of the array argument
Example:
public void PrintArray(int [] array)
{
int i;
for(i = 0; i < array.length; i++)
{
System.out.println(i);
}
}
Note that arrays are always passed by reference
ICE : Initialize all the elements of an array to be equal to their subscript (index)
Returning An Array From A Method
2 parts:
3. Declaring the return type
4. Returning the array
Step 1:
In the parameter list, put the [] after the type, similar to declaring one as a variable
Step 2:
BIT 115
BIT 115
Page 2 / 3
BIT 115
Arrays
Page 3 / 3
Make sure NOT to include () after the name of the array argument
Example:
public int[] CreateArray(int howMany)
{
int [] newArray = new int[howMany];
return newArray;
}
Note that arrays are always passed by reference
ICE :
Public, Private, Protected
public: anybody can call this method
Good for methods that we want to make widely available
private: nobody (except other methods in the same class) can access this
Good for helper methods
Point out the "helper method" pattern in the book
HOWEVER, private methods aren't made available to classes that inherit from this class, either
(subclasses)
protected: like private, except that subclasses can call the method
<< Open up the documentation for the Robot class, and point out stuff like besideThing() >>
In general:
declare everything private
UNLESS you want to make it available to Anybody ( public)
UNLESS you want to make it available to subclasses (protected)
Why would you want something that's private?
Other parts of the program can't manipulate.
Fewer bugs, but isn't any more secure.
The main advantage is that other code won't accidentally change your object's internal data.
This idea is called encapsulation: the internal, private, state of an object isn't accessible to other
parts of the program.
ICE: public vs. private
More Design
Go over the CastleWalker exercise.
BIT 115
BIT 115
Page 3 / 3
Download