1

I am creating a method that returns the variable elt if it exists in an array. If it does not exist in an array, I need to return null.

The issue is, I am checking for the variable elt in each item in the array using an if statement in a for-loop. I do not think I can put a return statement at the end of the if statement in the for-loop, because each time it executes the if statement, the potential return value would be different. I think this would make a new return value each time the for-loop was looped through. To solve this, I created a boolean temp variable called exist. If true, the method will return the variable elt. If false, it will return null. The code I am working with is below.

public T remove(T elt) {
        boolean exist;

        for (int i=0; i<data.length; i++) {
            if (data[i] == elt) {
                data[i] = null;
                size--;
                exist = true;
                System.out.println(exist);

                for (++i; i < data.length; i++) {
                    data[i-1] = data[i];
                }
            }
        }
        if (exist = true)
            return elt;
        else
            return null;
    }

My question is, is there a way to tuck the return statement in the method without using a temp variable?

8
  • 2
    It's perfectly fine to put a return in a for loop. The method will finish and exit as soon as you return. Commented Oct 20, 2015 at 14:13
  • Note that your posted solution and the accepted solution differ in that yours removes all occurrences of elt in data, while the accepted solution only removes first encountered occurrence (and exits immediately). This might be good or bad depending on your method's contract. Commented Oct 20, 2015 at 14:29
  • @JiriTousek, thank you for pointing this out, you are absolutely right! Though, it seems even my posted solution only removes the first encountered occurrence as well. Can you give me a hint on how I can get it to remove all occurrences? I am not able to figure it out. Commented Oct 20, 2015 at 14:33
  • @OmarN one problem I see is that when you delete an occurrence, you shift remaining items using the i varible, moving that one to the end of array in the process. Not a good practice by the way, I recommend you don't touch the iteration variable unless necessary. Second thing, you shift all remaining items left, so so you'll skip testing / removing the former (i+1)-th element - that could be fixed by replacing the if with while. Commented Oct 20, 2015 at 14:38
  • Also I'll repeat what I wrote in mistake in comment to one of the answers - if (exist = true) will always evaluate to true, since exist = true is an assignment - you'll want == there instead of = if you're trying to make that code work. Commented Oct 20, 2015 at 14:46

2 Answers 2

3

You can put a return statement almost anywhere.

To answer your question, you could put a return after your inner for loop like follows:

public T remove(T elt) {

    for (int i=0; i<data.length; i++) {
        if (data[i] == elt) {
            data[i] = null;
            size--;
            System.out.println(exist);

            for (++i; i < data.length; i++) {
                data[i-1] = data[i];
            }

            return elt;
        }
    }

    return null;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help! To me, this looks like it will return both elt in the for-loop, and then return null once it exists the for-loop. Isn't that how it would work? In which case, I do not understand how it can return two things.
It cannot reach both return statements. Once you reach a return statement, the code leaves the method and "returns" to the code that called it (aka: going back up the stack). You can only have 1 return value. Any code after a return will not be executed.
3

There is no need to use one extra variable . You can directly return from for loop. Instead of writing exist = true write return elt and at the end instead of

if (exist = true)
            return elt;
        else
            return null;

just write return null so that if elt doesnt exist it will return null.

3 Comments

What about returning null if elt does not exist? Will writing return elt return null automatically if elt does not exist?
@Jiri I copy pasted his code :). Anyways that code needs to be remvoed
@Rehman Errr...it was...it was a squirrel on my keyboard, definitely not me writing such a dumb comment. :)

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.