Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
The Java language includes a number of syntax styles that allow you to loop through the contents of arrays and other types of data collections. I'll demonstrate four approaches to looping in this project named Loops. The main class in this project has a set of code that's commented out currently, it declares a static variable, an array of strings called months. I'm going to loop through this data and output these strings a number of times using a different syntax style each time. I'll start by uncommenting this code.
This sort of loop uses a temporary variable, sometimes called a counter variable. When you generate the code using Eclipse, it will name the variable i for integer and it will datatype it as an int. Now if you're doing with a fairly small amount of data, you could change the data type to a short or even a byte, but typically ints are used in this case. When you're looping through an array, you should set the initial value of the counter variable i as 0. This is because in Java arrays are 0-based.
The first item is item 0. The second is item 1 and so on. Then in order to make sure that you hit every item in the array, you add a comparison. You look at the counter variable I, and you compare it to the length of the array, as long as i is less than the length, you'll execute any code within the for-loop. But when i matches or exceeds the length, you'll stop, and again, that's because of the 0-based array. Say you have an array of 10 items, if the length is 10 and i starts at 0, then you can keep executing code as long as i is less than 10.
When it actually is 10, it's exceeded the available items in the array. The third part of the for construct is the increment. Most commonly, you'll see this, i++, meaning, increment the value by 1, but you can put in any operation you want here. You could use i--, you could say i+ = 5 and then you would be stepping by 5, and so on. So now I'll place the cursor inside the for-loop, and put in System.out.println and I'm ready to output something.
Now when Eclipse generated the code, it thought I might want to output the values from args, but instead I'm going to output the values for months and that's the array that I declared at the top. And in the print line command I'll output months, open bracket, i, closed bracket, and now I'm going to be outputting the string at that position of the array. I'll Save and Run the code and there is the result. Now to make this a little bit easier to see, I'm going to detach the console right-click and choose Detached, move it up onto the screen and expand it, and now I have a lot more room to see the output from my application.
So that's a for loop with the counter variable. Here's another approach to using a for loop. This is called a for-each, but unlike some C Style languages, you don't actually use the word each. Here is how it actually looks. Once again I'll type in the word for, and press Ctrl+Space and I'll for-each from the list of choices and here's how Eclipse models it. Within the for-loop it declares a variable by default of type string, and it gives it a name. I'm going to change the name from string to month and then it asks what array or collection you want to loop through.
I'm going to be looping through months. So in English this would be read as, for-each month in the month's array, but you declared in Java using a data type variable before the colon and the array or collection you are looping through after the colon. Now I can refer directly to the variable month. So I am going to go make a copy of this code, System.out.println, I'll paste it in, but now I don't need refer to the collection anymore, I'm already doing the iteration, I'll just refer to the variable that's created temporarily each time through the loop.
I'll go up to my original for loop and comment it out and I'll Save and Run the application, and once again there is my output. In most cases with the arrays, you can use either of these two approaches. The only real advantage to using the counter variable, as opposed to the for-each syntax is that you're creating new counter variables, rather than complex objects. Whereas, with the for-each, particularly with the strings and other complex objects, you are creating a new object on each time through the loop. In most environments it really won't matter, and you should use the syntax that you're most comfortable with.
I'll comment this version out, and now we'll move to the while loop. There are two forms of the while loop, the while loop where you put the condition at the front and the while loop where you put the condition at the back. Before you create a while loop, you should declare a variable and give it an initial value, unlike the for-construct, the while construct does not do the variable tracking for you. So I'll create a variable called counter, and set it to a value of 0. Now I'll type in the word of while and I'll press Ctrl+Space and I'll choose while loop with condition.
The while loop with condition takes a look at a Boolean expression and asks
should I keep on looping?
I'm going to set my condition as counter
And once again it does exactly the same thing as the for-loops, but with a slightly different syntax. Finally, there is the do-while loop. For the do-while loop you place the condition at the end of the loop. I'm going to make a copy of this code and I'll paste it down at the bottom. I'll comment out the original and now let's create a do-loop. I'll take this while command and I'll move it after the code block. Then I'll the place the cursor at the beginning of the code block and put in the word do, and after the while command, I'll finish with a semicolon.
So now I'm saying start by outputting the value and then increment the value and then do the evaluation rather than doing the evaluation at the beginning. Take a look at what happens. It still works exactly the same. So these are the four possible syntax styles that you can use for looping through arrays and other collections. You can use the counter for loops, you can use for-each syntax, you can use a simple while-loop or you can use a do-while loop. In many cases it's just a matter of program or preference, which syntax you like, or are most comfortable with.
You will find that there are differences in what problems you can solve between say a while and do-while, but for the most part it's your choice.
Find answers to the most frequently asked questions about Java Essential Training (2011) .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.