~~8.1 Arrays~~ - as many people have noticed, making a bunch of variables for the same type of object is a pain - for example, what if you wanted to have 4 or 5 balls in pong? ball1, ball2, etc. seems a bit excessive - the way to get around this is to use arrays (or an ArrayList) - an array serves as a container of data, it stores a bunch of pieces of data in one easy-to-access place - think about having a bunch of cds thrown around on the floor... not pretty - that's sort of like what happens out in memory when you declare a bunch of variables... hard to manage - now think of putting them all in a CD rack, much easier, right? - that's sort of how an array works - making an array is similar to making an object in that you use 'new' - new double[ 10 ] for example makes an array of doubles with 10 slots (so it can hold ten numbers) - when declaring a variable you must put [] on the data type too, so a full declaration assignment looks like this: double[ ] array = new double[ 10 ] ; - you can declare an array of any type, from primitives like double or int to objects like string or sprites - that's how you declare one, but how do you put data in it? - when you declare an array default values are placed into all the slots, 0 for numbers, false for boolean, and null for Objects - remember each slot in an array is an individual value - so you assign things to them like any other variable, with just a little twist - you must use [ ] with some number in between them to denote which slot you're using - so for our example, array[ 0 ] = 20.1 ; would assign 20.1 to the first array slot - why 0? no idea, that's just how computer scientists do things - because we start with 0 the indexes only go to 9 - if you say array[ 10 ] you'll get a run-time error - this is another example of the "off by one" error I mentioned in looping - access to the data is done similarly, if we wanted to test if the value is over 20 we would do: if( array[ 0 ] > 20.0 ) doSomething( ) ; - note: remember using <,> is okay with doubles, but not == - if you need to check the length of an array you can say array.length (with no parentheses, when we use length( ) on Strings it is a method call, but arrays are not objects and so cannot have methods) - some problems with arrays 1. they have a set size that cannot change 2. each slot *must* have a value in it, which is why arrays get default values as mentioned above (why would this be a problem?) 3. adding and removing is a complete hassle - how would you do this? - the only solution is to create a new array of larger/smaller size and copy the elements individually with a loop ~~Side Note~~ - arrays (and ArrayLists) are a great example of the power of loops - because you need to specify which slot in the array you want to access, you can use the counter variable from a loop to do this - let's say we have the following double[ ] salary = new double[ NUMBER_OF_EMPLOYEES ] ; - this is an array to store the salary of the employees at a company - NUMBER_OF_EMPLOYEES is a constant of int type set up as an instance field (remember: private final int NUMBER_OF_EMPLOYEES = # ; is how it would be declared in the class) - also assume that the salaries have already been set somewhere else in the program - what if we want to give everyone a $5000 raise? for( int i = 0 ; i < NUMBER_OF_EMPLOYEES ; i++ ) salary[ i ] = salary[ i ] + 5000 ; - alternativelty you could have used salary.length instead of NUMBER_OF_EMPLOYEES - remember I mentioned "off by one" errors with looping? here's where they come into play - if we had started with i = 1 what would be the problem? (we would miss the person in slot 0) - Java also has a shorthand way of writing a for loop for arrays (and ArrayLists) - instead of the way written above it could be written as: for( double slot : salary ) slot = slot + 5000 ; - this syntax is becoming very common for looping over arrays - the "double slot" defines a new variable only accessible in the for loop - the loop goes through every element of salary in order, each time assigning it to the slot variable - it is basically a way of shortening the for loop and keeping you from typing [ i ] +[ Self Check pg 283 ]+ 1. What elements does the array contain after the following statements? double[ ] data = new double[ 10 ] ; for( int i = 0 ; i < data.length ; i++ ) data[ i ] = i * i ; ~~8.2 ArrayList~~ - arrays are a fairly primitve construct (not primitive as in primitives & objects, primitives as in caveman primitive) - the worst part is that they are a set size and that size must be specified at creation time, which may sometimes be a hassle to know beforehand (for example if the size depends on some user input or events out of the programmer's control) - to remedy this, more modern container types have been invented that are based on Objects - the most common in Java is an ArrayList - it fixes the three problems with arrays mentioned above 1. sizing is dynamic, the ArrayList will expand or shrink as needed 2. because the size is dynamic, there aren't empty slots with default values out there, all data is set and there because you want it to be 3. inserting and deleting elements is easy and provided by built-in methods - to create an ArrayList of doubles: new ArrayList< double >( ) - an assignment initialization would look like this: ArrayList< double > salary = new ArrayList( ) ; - note we do not need to specifiy a size (although you can if you want) - to add values you call the add method: salary.add( 50000 ) ; --> adds 50000.0 onto the end of the salary ArrayList - if our ArrayList holds objects--let's say OvalSprites--you have two options: spriteList.add( new OvalSprite( 1, 1 ) ) ; --or-- OvalSprite s = new OvalSprite( 1, 1 ) ; spriteList.add( s ) ; - to access values you use the get method: salary.get( 0 ) ; --> gives back 50000.0 spriteList.get( 0 ) --> gives back the OvalSprite( 1, 1 ) - get is basically a replacement for [ ] that we used on arrays - to insert something at a particular position use add with two parameters, the first being the index and the second being the value - note: if you give it a position that is longer than the ArrayList it throws and error just like arrays do - adding at some position makes the current one at that position and everything after increase their indices by one - example: (assume spriteList is of length 10 now) spriteList.add( 6, new OvalSprite( 3, 2 ) ; - removal works similarly, it just takes the index of the position to remove - how do you know the size of an ArrayList? - unlike arrays, you cannot say spriteList.length - unlike Strings, you cannot say spriteList.length( ) - you must call spriteList.size( ) +[ Exercise ]+ - create an ArrayList of integers of size 5 - fill it with values using add - come up with a way to find the highest value inserted ~~Other Notes~~ - might be a good idea to read how to 8.1 on page 301 ~~8.6 Two-dimensional arrays~~ - you can also have an array of arrays (or ArrayLists) - this is what's known as a 2D aray - you can have more dimensions also - for arrays: double[ ][ ] array = new double[ 10 ][ 10 ] ; for ArrayList: ArrayList< ArrayList< double > > array = new ArrayList< ArrayList >( new ArrayList< double >( ) ) ; - multi-dimensional arrays have many uses - grid layouts (like a chess/checker board) - they are a good to use for networked games too - say we want a player to be able to shoot bullets - that is easy enough, give them an ArrayList that you add and remove from as they shoot and hit things - what if we want it to be multiplayer? - well you make the "outer" array the size of the number of players, and "inner" array the list of bullets for that player +[ Review Exercises pg 312 ]+ R8.1, R8.3, R8.4, R8.7ab, R8.9, R8.17, R8.18