CS107 References and Arrays By Chris Pable Spring 2009.

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

An introduction to pointers in c
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
C Arrays and Pointers In Java, pointers are easy to deal with –In fact, there is little that can go wrong in Java since pointer access is done for you.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Hashtables David Kauchak cs302 Spring Administrative Talk today at lunch Midterm must take it by Friday at 6pm No assignment over the break.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
ArrayList, Multidimensional Arrays
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
Data structures Abstract data types Java classes for Data structures and ADTs.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Variables, Primitives, and Objects A Visual Learner’s Guide.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Hashtables David Kauchak cs302 Spring Administrative Midterm must take it by Friday at 6pm No assignment over the break.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
int [] scores = new int [10];
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
CSE 351 Caches. Before we start… A lot of people confused lea and mov on the midterm Totally understandable, but it’s important to make the distinction.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Lesson 2: Building Blocks of Programming
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Java Coding 6 David Davenport Computer Eng. Dept.,
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Presentation transcript:

CS107 References and Arrays By Chris Pable Spring 2009

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”

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

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

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

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

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

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;...

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;...

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);

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

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!

Real world example soon to come!

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?

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

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

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.

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.

Picture Time!

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.

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?

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!

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);

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?

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!

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

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

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!

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!

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 }

Questions?

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 :)