Skip to 0 minutes and 11 secondsAt the end of the last week, we had a smiley face on the screen. I hope you did. If you didn't, go and get the code and copy and paste it in so that you will have so we have the same code to work from. We'll start looking at arrays of variables and repetition. So we know that we can declare a variable, and we've, for instance, seen a variable referring to a bitmap, so we can have more complex things than just numbers. Let's imagine that we had a variable that was referencing a book, for instance. So we get the page numbers, how many pages there was in the book, what is the text in the book.
Skip to 0 minutes and 49 secondsWe could get that, and that would work. But what would we do if we had a library with millions of books? Would we then declare a million variables that could each refer to these books? That would be stupid. There must be a better way, and thankfully, there is. It's called arrays. So in arrays, we just line up the different books, the different variables. So we can actually go in and say, get me the first book or get me the fifth book, get me the fifth value, and we can start using all of the books in this array of variables.
Skip to 1 minute and 34 secondsWhen we have all these, it would be very nice to be able to go through them all and do something with them, and that's where looping comes in. We can actually say, with all the books in this array, get us the text of the book and present it on the screen. It's a very convenient way of working with the variables. Basically, what looping is, is just a conditional. So conditions was last week, but instead of just having one block of code, what we do here is say, if there is a situation, let's keep looping through until that situation has finished, and then we'll stop looping.
Skip to 2 minutes and 12 secondsWe will learn about these things in more depth and we will use them to introduce new features in our game.
Learn the basics of arrays
In this video you’ll learn the basics of arrays and how they can be used to improve the game. There will be an introduction into how repetition is used in programming, and why this is useful. In addition, you’ll also find out how these new constructs will be put into practice in the game.
You might like to download the Setting up your code guide, which will take you through how to set your code up for the next few activities. You’ll want to replace TheGame.java with the code in v4.java file.
© University of Reading