0

Write a Java method that recieves an undeclared ArrayList of integers and an integer as parameters, searches the ArrayList for the integer, returns true if found.

A)Use a while loop to do the searching. B) Try it again but with a for loop C)Try again, but with for-each loop D) (this is hard) Write a Java method that recieves an ArrayList of integers as a parameter and returns the index of the largest integer in the collection.

//part a 
int i =0 ;
while (i<500) 
{
    if (values [i] == 3927)
    { 
        system.out.prinln("FOUND IT!");
        break;
    }
    i++; 
}  

If needed , ill post the code I came up with, please comment if need. I really am having troubles getting this to compile.

9
  • can you post the code Commented Feb 22, 2014 at 18:45
  • 5
    Sure post the code that you've tried. You should always do that. Commented Feb 22, 2014 at 18:45
  • If needed? You should probably read some stuff in the help section. You don't order code. Commented Feb 22, 2014 at 18:45
  • 6
    People here, can we stop downvoting and give OP a chance to post his code? He has clearly mentioned that in question. This is not a nice welcome to a new user. Commented Feb 22, 2014 at 18:46
  • 1
    +16 reputation for what is essentially "please code this for me", not bad... In a question like this, you should really show the code you have and tell what it does and outputs and tell what is wrong with it. Then you'll quickly get help, usually. Commented Feb 22, 2014 at 19:04

3 Answers 3

1

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!!!!)

Sign up to request clarification or add additional context in comments.

Comments

0

Well the question asks you for part A to write a method that returns true or false so you are missing that part of it, also you're using an array and not the ArrayList class, your code should look something like this:

public static boolean search(ArrayList<Integer> list, int key) {
    int i = 0;
    while (i < list.size()) {
        if (list.get(i) == key) {
            return true;
        }
        i++;
    }
    return false;
}

Comments

0

Here's case A. You should try to figure the rest out on your own if you plan on learning anything.

public boolean contains(ArrayList<Integer> haystack, int needle) {
    if (haystack == null) {
        return false;
    }
    // case A
    Iterator<Integer> it = haystack.iterator();
    while (it.hasNext()) {
        if (it.next().equals(needle)) {
            return true;
        }
    }
    return false;
}

2 Comments

Why do you need to use an Iterator? Haystack isn't being modified.
The OP needs to use a while loop and this seems like the best way to go without maintaining a separate counter variable. But yes, I normally wouldn't use an iterator, or a while loop, in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.