English Conundrum s In English, add “s” to end of word to make plural s But for 1 special word, adding an “s” at the end: Makes word go from plural to singular Makes masculine word into a feminine one What is this special word?
English Conundrum
Announcements
Primitives vs. References Primitive VariablesReference Variables Variables hold actual value Assignment copies value Update variable being assigned only Aliased with assignments Both refer to same instance Both see updates to object Re-assigning variable does not update aliases
Opening Question Are array variables primitives or references?
Array Variables Can be set to null to mark there is no array Cannot use until after have instantiated array int[] bob = new int[30]; Car[] parkingLot = new Car[300]; Variables aliased by any assignments Can deduce, array variables always references May hold primitive data, but these are separate issue
Array Variables Can be set to null to mark there is no array Cannot use until after have instantiated array int[] bob = new int[30]; Car[] parkingLot = new Car[300]; Variables aliased by any assignments Can deduce, array variables always references May hold primitive data, but these are separate issue
Memory Trace Example int[] data = new int[3]; int[] al = data; for (int i = 0; i < data.length; i++) { data[i] = i * 2; } al[0] = 5; al = new int[1]; al[0] = 7;
Entries Are Variables, Too! Within an array entries like individual variables int[] bob = new int[30]; Car[] parkingLot = new Car[300]; bob ’s entries would be primitive variables entries parkingLot ’s entries refer to instances Entries initialized like variable when array created entries bob ’s entries would all be set to 0 entries null stored in all entries in parkingLot
Memory Trace Example Car[] data = new Car[3]; Car[] al = data; for (int i = 0; i < data.length; i++) { data[i] = new Car(); } al[0] = new Car(); al = new Car[1]; al[0] = data[0]; data[0] = data[1];
Higher-Dimension Arrays Arrays instantiated with any dimension desired 2-d array is table
Higher-Dimension Arrays Arrays instantiated with any dimension desired 2-d array is table identifying entries by row & column int[][] pixels = new int[20][100]; pixels[1][18] = 32;
Higher-Dimension Arrays Arrays instantiated with any dimension desired 2-d array is table identifying entries by row & column int[][] pixels = new int[20][100]; pixels[1][18] = 32;
Higher-Dimension Arrays Arrays instantiated with any dimension desired 2-d array is table identifying entries by row & column int[][] pixels = new int[20][100]; pixels[1][18] = 32; Entries stored in virtual cube with 3-d array int[][][] bert = new int[60][4][10]; bert[45][3][7] = 42; Rare to use higher dimensions, but is possible
Computer Professor MemoryHuge (> 250GB)Can’t remember 18 names Computing speed Fast (> 2 billion/second)Slow (needs fingers & toes) Takes direction Does exactly as toldLeaves toilet seat up Speed of updates Nearly instantaneousWears t-shirts from 1989 Ability to plan & reason Cannot make plans; No reasoning skills; Lacks common sense Makes (semi-)complicated plans; Good reasoning skills; Lacks common sense Computer vs. Professor
People are very good at reasoning & analyzing Slow computing outcome of each potential decision Considering every possible data combinations hard Trouble when lots of data available to be examined Computers can perform simple tasks quickly Maintain billions of data items within memory Very good when at performing simple operations Cannot create plan since lacks concept of future To do any task, must be given specific instructions People vs. Computers
Computers best processing huge data sets Perform specific tasks and output results: College’s students’ transcripts available Compute & print receipt for groceries Filter & provide few items for user to choose from Personalized recommendations from Amazon Google’s list of matching web pages Most of year examines how to organize data How data used determines structures selected Computers Use Collections
How Could We Do This? Know simple way to organize data: array Entries refer to another array to create matrices Many limitations arise when using arrays Must specify unchangeable size when created Pirate[] rum = new Pirate[1]; Pirate[] h2o = new Pirate[ variable ]; rum = new Pirate[60]; // old array was lost! h2o = rum; // h20 now alias to rum’s instance Waste memory requiring maximum size for array /* Each Amazon.com customer uses 400+MB of RAM! */ Rating[] hertz = new Rating[ ];
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Often need to keep values ordered or organized Alphabetical list of names & phone numbers Keep list of job bids from smallest to largest Web pages ordered from most to least important Desired index’s not just assigned new value Lose current value stored at that index Instead, must first shift values within array Inserting into an Array 12 n e 0
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Still need to keep values ordered or organized Removing a value presents own set of problems Could simply assign null, but that creates a gap Finding start & end of data hard once gaps allowed Instead, must shift values to fill in all gaps that exist Removing From an Array 0 12 n
Your Turn Get into your groups and complete activity
For Next Lecture Moving to review OO concerns next week How do we write & use classes on our own? Instances can differ, but how is this expressed? What is an instance variable & how is it used? What does static mean? When would we want it? There is weekly assignment problem on Angel Due before Wednesday’s lecture (via Submitter) Get back into the swing of writing Java code