Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java for loop tricky iteration

I've run the code multiple times wondering what's wrong.

this simple for loop should run 4 times. why is it running only twice?? Did i miss anything. I'm using android-studio stock emulator api-28 for debugging.

boolean[] booleans = {false, false, false, false};

for(boolean b :  booleans){

     System.out.println(b);
}

log cat output

please check the edited code and log cat output.

edited code

like image 534
Karan Vadde Avatar asked Dec 29 '25 19:12

Karan Vadde


1 Answers

At first glance, in Android Studio it prints twice or 3 times when debugging, in the Debug tab, but
the truth is this:

I/System.out: Start
I/System.out: false
I/System.out: false
I/chatty: uid=10075(u0_a75) com.xxxxxxxxx.xxxx identical 1 line
I/System.out: false
I/System.out: End

or

I/System.out: Start
I/System.out: false
I/chatty: uid=10075(u0_a75) com.xxxxxxxxx.xxxx identical 2 lines
I/System.out: false
I/System.out: End


as you can see AS instead of printing

I/System.out: false

prefers to print

I/chatty: uid=10075(u0_a75) com.xxxxxxxxx.xxxx identical 2 lines


Why? I can't say.
Maybe something similar happens in the Logcat tab.
Yes it is similar in the Logcat tab:

2018-12-04 14:16:17.756 2209-2209/com.xxxxxxxxx.xxxx I/System.out: false
2018-12-04 14:16:23.902 2209-2209/com.xxxxxxxxx.xxxx I/chatty: uid=10075(u0_a75) com.xxxxxxxxx.xxxx  identical 2 lines
2018-12-04 14:16:24.929 2209-2209/com.xxxxxxxxx.xxxx I/System.out: false
2018-12-04 14:16:25.860 2209-2209/com.xxxxxxxxx.xxxx I/System.out: End

So finally: remove filtering of your output window by deleting "System" and you will see output either like

I/System.out: false

or

identical 2 lines
like image 53
forpas Avatar answered Dec 31 '25 09:12

forpas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!