Okaaaaaaaaay. Let's go from the top my good man.
The While Loop
Documentation: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
The while loop is a statement that says the following:
while the current statement is true
keep doing everything inside the brackets.
So for example..
while(!found) // While found is not equal to true
{
if(x == 4) found = true;
}
As soon as x equals 4, the loop will end. These loops are designed when you don't know how many times you will loop. Following this example, you would need to know some details. First, you need to know what the user is looking for, let's call this value. Second, you need the list to search, let's call this myList. You're returning a boolean, so your method is going to look like this:
public boolean exists(Integer value)
{
int position = 0; // The position in myList.
while(!found && position < myList.size())
{
// You're going to work this bit out.
}
return false; // Value doesn't exist.
}
The trick here is, to set found to true, if the value in myList, at position position, is equal to value.
The For Loop
Documentation: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
The For loop is usually used, when you know how many times you want to loop. However, as this is an academic exercise, we'll just ignore that little detail for now. The idea of a for loop is as follows:
for(some value x; check x is less/more than some value; do something with x) {
}
So for example:
for(int x = 0; x < 10; x++)
{
System.out.println("Hello: " + x);
}
The above loop will print out Hello:0, Hello:1... Hello:9. Now, what you want to do is do the exact same thing you did in the while loop, but just wrap it up in a for loop..
for(int position = 0; position < myList.size(); position++)
{
// if value, in myList at position equals value, then return true.
}
return false; // Value doesn't exist.
The For-Each Loop
Documentation: http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
The for-each loop is very similar to the for loop, only the syntax is a little bit nicer, especially when you want to loop through every value in a List or Array.
for(Integer item : myList)
{
// Creates a variable called value. If item == value, return true.
}
return false;
As for the last one, it's all on you buddy, but I'll tell you some tips. You're going to be looping through every value in your List (SEE ABOVE!!!!)