tailieunhanh - head first java second edition phần 3

Bắt đầu với Java (Tiger), ngôn ngữ Java có một loại thứ hai của Jor vòng lặp được gọi là JM tăng cường, làm cho nó dễ dàng hơn để lặp qua tất cả các yếu tố trong một mảng hoặc các loại khác của bộ sưu tập (bạn sẽ tìm hiểu về othercollections trong chương tiếp theo). | enhanced for The enhanced for loop Beginning with Java Tiger the Java language has a second kind of for loop called the enhanced for that makes it easier to iterate over all the elements in an array or other kinds of collections you ll learn about other collections in the next chapter . That s really all that the enhanced for gives you a simpler way to walk through all the elements in the collection but since it s the most common use of a for loop it was worth adding it to the language. We ll revisit the enhanced for loop in the next chapter when we talk about collections that aren t arrays. Declare ar iteration variable The Col that will hold a single element Mea m the array. for String name name Array VJ w th each iterator a dif-ievent element in the array will be assigned to the variable name elewih-ts in array yWT be pa-tible wi-th -the deilared variable -typ tee ted hi . __. ivu. vou wini ver The edlei-ton Fred and with the What it means in plain English For each element in nameArray assign the element to the name variable and run the body of the loop. How the compiler sees It Create a String variable called name and set It to null. Assign the first value in nameArray to name. Run the body of the loop the code block bounded by curly braces . Assign the next value in nameArray to name. Repeat while there are still elements in the array. Note depending it Part One Iteration variable declaration Use this part to declare and Initialize a variable to use within the loop body. With each Iteration of the loop this variable will hold a different element from the type of this variable must be compatible with the elements in the arrayl For example you can t declare an nt Iteration variable to use with a Strìngũ array. Part Two the actual collection This must be a reference to an array or other collection. Again don t worry about the other non-array kinds of collections yet you ll see them in the next chapter. 116 chapters Converting a String to an

TÀI LIỆU MỚI ĐĂNG
6    117    1    26-11-2024