java - How would I remove an index from ArrayList after a return statement? -


i working on class lab , ran trouble. have been asked write method following instructions:

"dog getrandomdog() - randomly selects dog, returns it, , removes kennel. returns null if there no dogs."

this method wrote (which doesn't work):

public dog getrandomdog(){     if(dogs.size() >= 0){         random random = new random();         int index = random.nextint(dogs.size());         return dogs.get(index);         dogs.remove(index);    }     else {     return null;    } } 

i understand cant have executable statement after return, how heck around this? in advance.

i come old school of thought one entry, 1 exit

public dog getrandomdog(){     dog dog = null;     if(dogs.size() >= 0){         random random = new random();         int index = random.nextint(dogs.size());         dog = dogs.remove(index);     }     return dog; } 

this means allow 1 point of entry method (a little mute in java) , 1 exit (or 1 return). makes method easier understand don't need worry method exiting half way through. pain when method can run several screens lengths...


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -