简体   繁体   中英

How do you access an array or variable declared or initialized in a nested loop from outside of the loop?

How do you access an array or variable declared or initialized in a nested loop from outside of the loop?

You cannot. You must declare the array outside the loop. This may seem strange at first but what if the condition for the loop to run even once is never met? How could you possibly then access an array which was never even created?

You cannot. Java uses block scope, so variables declared within any block (such as a loop) are not accessible from outside. You'll need to assign them to something that has an outer scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM