![]() public class AdvancedForLoopDemo įor (int i = 0 i < students. ![]() It creates a String array and then iterates over it using the loop. but needs to use it on Java 1.4 or needs to access the iterator or. If the type of variable and collection are not compatible, then compiler will raise an error, thus saving the program to break at run time.Īn example of enhanced for loop is given below. Ive often written iteration code using the enhanced for-loop initially. If the collection is of some class type, then the variable’s data type should be of same type or its super-type.Įxample, if the collection is a list of Cat, then variable can be of types Cat or Animal where Animal is the super class of Cat. Remember that the data type of the variable should be the same as the collection’s data type. 20AugKatrajJavaBasics / src / arraydiscussion / EnhancedForLoop.java / Jump to. Thus, in first iteration, variable will have the first element, in second iteration, it will have second element and so on. In every iteration, the variable contains a collection’s element. Variable is a user defined name which holds a value.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |