Week 9, Notes 2

Getting information from the API documentation

The Java Application Programming Interface (API) lists all the classes that are supplied with the Java Software Development Kit (SDK). 

 

In JGrasp, if you have a java file open, the Help menu shows a link to the API.

 

The API shows 3 frames:

Top left – the list of java packages.  Each package contains a set of classes.

Bottom left – the list of classes defined in the package selected in the top right frame.

Right – a description of the class selected in the bottom left, containing:

·         Class description – general information about what objects of the class do

·         Field summary – public constants, e.g., Math.PI.

·         Constructor summary – lists all the constructors and the parameters they take.

·         Method summary – lists public methods and parameters they take.  The left column shows the method return type.

Example:

Use the API to find a method that would help if I wanted to find out if a line input by the user started with the string “Hello”.

 

Arrays provide a mechanism for determining their length

All arrays contain an instance variable named length

Given an array, the number of elements in the array can be determined by making a reference to the length instance variable.  Notice if x is an array, you type x.length to get the length of the array, but if x is a String, you type s.length() to get the length of the String.

Searching arrays

‘Walk’ an Array using a for loop

Walk means visit each element in the array.

for loops work well for arrays, because Arrays are fixed size, and for loops only work well when you know how many times you want to loop.

Search for an element

Given a value, walk the array and check each element to see if it is equal to the value we are searching for.

We will write ../demos/W09N03.java in class