Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS107 References and Arrays By Chris Pable Spring 2009.

Similar presentations


Presentation on theme: "CS107 References and Arrays By Chris Pable Spring 2009."— Presentation transcript:

1 CS107 References and Arrays By Chris Pable Spring 2009

2 Recall last time... Square mySquare = new Square(); Data Type Instance Name (Reference since this is a Class) Assignment Operator “The Chain” (References need to be linked to a memory address) Goes into memory and finds a block the size of a square, gives the memory address to be linked to Places all the variables needed for a square in the memory space Provided by “new”

3 So What Just Happened? Program Memory mySquare “new” made this! int x; int y; int size;... Nowhere

4 So What Just Happened? Program Memory mySquare “new” made this! int x; int y; int size;... The Equals Sign

5 What If we ran: mySquare = new Square(); After already pointing to a Square?

6 What will happen? Program Memory mySquare int x; int y; int size;... The Equals Sign “new” made this! int x; int y; int size;...

7 What will happen? Program Memory mySquare int x; int y; int size;... The Equals Sign “new” made this! int x; int y; int size;...

8 We can no longer do anything with the orange square. Nothing links to it (Dereferenced)! Program Memory mySquare int x; int y; int size;... The Equals Sign “new” made this! int x; int y; int size;...

9 We can have more than one reference (link) point to the same instance of a class too! Program Memory mySquare int x; int y; int size;... The Equals Sign “new” made this! int x; int y; int size;...

10 After we did the previous code, imagine the following: We run this line of code: Square mySquare2 = mySquare; We have a method: Void manipulateSquare(square temp1) {..... } Invoked with manipulateSquare(mySquare2);

11 What happens? Program Memory mySquare int x; int y; int size;... The Equals Sign “new” made this! int x; int y; int size;... mySquare2 temp1

12 Why does this happen? You've actually seen this before! Remember deep and shallow copies. Those variables just hold the memory address of the data we are accessing! This is why shallow copies act funky. They actually copy the memory address these variables point to, not the actual data inside! Every time you see the word reference, think “LINK” like a chain. It will help you visualize it!

13 Real world example soon to come!

14 Arrays! You've heard so much about them, now it's time to show you why they rock! You may have used (a form of) them without knowing when you process user input in your programs. Lets look at your program... 9 Tiles..., lets say one of them has the label 5. How do we find it without having to write code that checks all 9 tiles?

15 First what does an array let us do? Lets us group together data of the same type Lets us iterate through every member of that group using an iterator instead of a unique identifier

16 What does an Array let us do? Easily Sort Multi-Dimensional Visualization (More on this later maybe? Think of it like a Map) Sending a bunch of data back at once easily

17 One problem with arrays... They can't (technically) grow. Though Java gives us a way to allow them to. Normally they are fixed in size.

18 Sample code (From Sun) int[] anArray; anArray = new int[10]; These can be combined: int[] anArray = new int[10]; Makes a list of 10 Integers in memory. Note: anArray is a REFERENCE to the memory where those 10 integers are stored! This is easy to see because you see the key word “new” being used.

19 Picture Time!

20 Putting things into the array Above, we see the array has 10 slots, 0 – 9. Lets say we want to put a number in slot 0. We type the reference name, anArray, followed by the slot number in square brackets. anArray[0] = 100; // Will set slot 0 to have the value of 100.

21 Using this array for automation Say we have the array: stuff = { 1, 2, 3, 4}. Such that 1 is in slot 0, 2 is in slot 1, etc. We run the code: int sum = 0; for (int i=0; i<4; i++) { sum += stuff[i]; } Whats the end result of sum?

22 Arrays Cont... If you got 10, your right! Java gives us a quick way to initialize all the elements of an array: int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}; This automagically runs key word new for us! Arrays can be of more than just type int. You can use doubles, chars, strings, booleans (all primitives). Even user made classes!

23 User made classes are special... (Code from wellho) Lets look at this code: Film Watch[] = new Film[4]; Watch[0] = new Film("Shrek",133); Watch[1] = new Film("Road to Perdition",117); Watch[2] = new Film("The Truth about Cats and Dogs",93); Watch[3] = new Film("Enigma",114);

24 Examining It... First we have an array of type Film, with 4 slots. “Film Watch[] = new Film[4];” In slot 0, we want to watch Shrek. Notice that new is called a second time Watch[0] = new Film("Shrek",133); What does this mean?

25 Each slot is actually a reference? In the example, Watch[0] points to a new Film instance. Remember, if something is equal to a “new” class instance, its a reference or link. That must mean, our array is actually a link to a list of other links for our own classes! How confusing! But it can be used to our advantage!

26 Array Data Members and Methods Arrays have probably the most useful data member associated with them you can think of... their length. Access it with: anArray.length They also have premade methods to sort, search, fill, equate and convert to an ArrayList (Not covered here). Access it with: anArray.Method() For more, see http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html

27 Arrays being passed to functions? Yup you can do it! For example: void takeArray(int[] array) { … } can be called by: takeArray(anArray);

28 A Note on Array Persistence Remember how I told you arrays are links to data out in memory? When you pass a variable, you pass a copy of it's contents. Since an array is a reference (a link), you pass a copy of that link, not a copy of the actual data! Hence, deep and shallow copy! Any data altered or permuted in the array when you pass it, will remain that way when it's passed back! Because of this, if your manipulating an array, you don't need to return anything! It will already be manipulated in memory!

29 Back to your program Now that we understand a bit more about arrays, lets go back to our question: 9 Tiles..., lets say one of them has the label 5. How do we find it without having to write code that checks all 9 tiles? Assume all 9 tiles are linked into an array called tiles[]. Frequently you will see many books denote an array by adding [] on the end of a variable name Talk with your partners then lets compare what you got!

30 Code (My Version Anyway...) Square getSquare(Square[] tiles, int label) {  for (int i=0; i<tiles.length;i++)  { if (tiles[i].getLabel().equals('”'+label)) return tiles[i];  } return null;//returns a link to nothing-ness }

31 Questions?

32 Real World Application References and arrays are used everywhere, not only do they save space by not making copies, but are useful ways handling interaction If we have time, I'll pull up Starcraft and do a small 2-3 min demo to show you them in action. Everybody loves Starcraft :)


Download ppt "CS107 References and Arrays By Chris Pable Spring 2009."

Similar presentations


Ads by Google