java - How to replace single element of array -


I have a series of random arrays.

  array1 [] = {1,2,3,0,0,5,6} array1 [] = {1,2,0,0,4,5,6}  

I want to end them like that, so I change the first 0 with x.

  array1 [] = {1,2,3, x, 0,5,6} array 1 [] = {1,2, x, 0, 4, 5 , 6}  

The code I am using is all given zero instead of just one.

  array 1 [] = {1,2,3, x, x, 5, 6} array 1 [] = {1,2, x, x, 4,5,6}  

Not what I see. I just want to be happy in place of one or just one using Code I,

 for  (int i = 0; i & lt; array.length; i ++) {If (fruit [i] == 0) fruit [i] = x; }  

Hope it was clear, thanks for any help!

Try to use the brakes.

 for  (int i = 0; i & lt; array.length; i ++) {if (fruit [i] == 0) {fruit [i] = x; break; }}  

This will ensure that only one change has been made, max.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -