Here is a declaration for a variable named args that will hold a pointer to any array holding String objects:
String [] args;
· Declare an instance variable that will hold an array of Strings.
· Write a constructor that:
o takes one parameter that represents the number of Strings the array should hold
o initializes the instance variable by creating an array that will hold the number of Strings specified by the user.
o puts a string in each array element.
· Write a method that walks the array and prints the value of each array element
(See ../demos/W10N01.java)
When we have an array of objects, we often want search the array for a particular object.
Doing this usually entails asking each object in the array to execute a method that returns some characteristic of the object.
For example, suppose we have a Dog class where each Dog stores its breed and weight.
Now we create a driver class that:
declares an array of Dog objects
asks the user for the breed and age of a dog
creates a new Dog object and inserts this object into the array.
Does the preceding two steps for as many dogs as the user would like
Asks the user for a breed and age and returns the first Dog object with the breed the user specified that is at least as old as the user specified.
You can rewrite any for loop as a while loop.
1. move the <loop counter initialization statement> to the statement before the while
2. change the for to while
3. move the <post body action statement> to the bottom of the loop body
4. remove the semicolons from inside the ()s
public void myMeth(int in)
{
// loop for the number of times passed in
for (int i = 0;
i < in;
i++)
{
System.out.println(i);
}
}