hey srthomso,
I would like to help more, but I need to understand how much you know and go from there. Have you learned about the for construct?
for (int i = 0; i < N; i++) {
//... loop contents here
// first loop iteration i = 0
// second loop iteration i = 1
// ...
// ends with i = N-1
// for a total of N times.
}
a for loop like this will execute for N times. This is equivalent to
int i = 0;
while (i < N) {
//.... loop contents here
i++;
}
Did you understand what I did in the function?
public static boolean function(int a) // <-- function that takes an integer and returns a boolean (true / false)
So put a loop in the funtion to iterate through all of array A. This function will print out all the values in array A.
public static boolean function(int[] A) {
for (int i=0; i<A.length; i++) {
// loop will execute A.length times
System.out.println(A[i]);
}
}
Now you want to search for an object in the array right? So a bit more logic to add... we'll add another function parameter that contains the value of the number to find in the array. We'll call this function parameter valueToFind.
public static boolean contents(int[] A, int valueToFind) {
for (int i=0; i<A.length; i++) {
// loop will execute A.length times
if (A[i] == valueToFind) {
// Hark! Mission accomplished we have found our value.
// therefore, we return true to tell the function that called
// this one that YES we have found a value
// The function immediately returns to the place you called it.
return true;
}
}
// We ONLY get to this spot in the code if we have gone through all
// the elements of array A, and none of them were the value
// we wanted. Mission failed. Tell caller that we did not find value.
return false;
}
Let me know what bit of knowledge you're missing. My advice to you is to simply try playing around with the code. Step through the code in a debugger if you have to. And I strongly encourage you to buy a good book and read up on the language.
Ed