April 11th 2014 public class Lecture6 1 Stack retrieves elements in reverse order as added 2 Queues retrieves elements in same order as added 3 Non contiguous memory 3 1 Array good for random access not very flexible 3 2 Spread in memory removing from the middle is very cheap change the connections between elements adding to the beginning is easy just say something is the beginning don t need to build a new structure can just add values and tell the previous value to go to that however traversing is not good get and indexOf is troublesome 4 References as fields 5 Null references null a value that does not refer to any object elements of an array of objects are initialized to null NOT the same as the empty string or the string null 6 Things you can do with null store null in a variable or an array element print a null reference ask whether a variable or an array element is null pass null as a parameter to a method some methods don t like null parameters and throw exceptions return null from a method often to indicate failure Dereferencing to access data or methods of an object done with the dot notation such as d length when you use a after an object variable Java goes to the memory for that object and looks up the field method requested timmy name toUpperCase goes to Timmy object goes to the name field goes to what name points to 8 Null pointer exception it is illegal to dereference null causes exception 9 Reassigning references when you say a next b next you say Make variable a next refer to the same valye as b next List node class public class ListNode int data Conscientiously not making them private ListNode next public ListNode int data this data data this data null public ListNode int data ListNode next this data data this next next public class ListNodeClient public static void main String args ListNode list new ListNode list data 6 ListNode list new ListNode 6 list next new ListNode builds new ListNode doesn t have a value list next data 42 goes to list goes to next new ListNode created goes to the data field sets it as 42 list next next new ListNode list next next data 12 goes to list goes to next 42 goes to next new ListNode created sets it as 12 ListNode list new ListNode 6 new ListNode 42 new ListNode 12 shorter hand way of building the exact same list
View Full Document