0

I'm trying to figure out how to add an item to an array based on the return value of a method.

Something like where in a method you can do return array.contains(value) and automatically return a boolean, but in a way where you can, instead of returning that value, add it to an array?

Is this possible in the same format, where you don't need a conditional statement?

Sorry for the noonish question, and thanks for the help!

2
  • If you don't want to return something, then why not just use a conditional statement? What do you mean? Commented Oct 1, 2017 at 4:03
  • You could work with streams. e.g. to take all numbers from array1 that are bigger than 5 and put them into array2 you could write int[] array2 = Arrays.stream(array1).filter(n -> n > 5).toArray() Commented Oct 1, 2017 at 4:20

1 Answer 1

1

I think what you mean is this:

if(array.contains(value)){
    array2.add(value)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah - This is what I had, I was just curious if it could be done in one line or not... thanks!

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.