Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 106 - Fall 2006 Today’s Topics Comments and/or Questions? Wrapper classes for the primitive types Linked lists

3 Wrapper classes An ArrayList actually stores references to objects, not the objects themselves so we cannot store values of primitive types directly -- we have to use the wrapper classes if we want to do that. The wrapper classes for the eight primitive types are: – Integer for ints – Float for floats – Double for doubles – Character for chars – Short for shorts – Long for longs – Boolean for booleans – Byte for bytes

4 Wrapper classes Until now, we've only used static methods in these classes To use the static methods in a class you do not have to instantiate that class --- that is, you don't need an object of that class to call the static methods. For instance, we used parseInt in the Integer class whose functionality was just to take in a String and return the int that is stored in the String. – int Integer.parseInt(String s) Similarly for – double Double.parseDouble(String s) And for the other Wrapper classes as well.

5 Wrapper classes But now, we need to instantiate these classes if we want a reference to the data for some reason (e.g. for use in an ArrayList.) Let's add code like this to our program and see that ArrayLists can store these: Integer i = new Integer(2); stuff.add(i); // necessary because a plain int is not a reference and //therefore is not able to be in an ArrayList One thing though, the way the wrapper classes are written for all the primitive types, there's no way to actually change the value of variable once it's instantiated. There are no “set” methods.

6 Wrapper classes Can anyone think of any use for maybe creating our own wrapper- like classes? For example, when else might creating an instance of a class that stores one int vs. just storing the int as an int, be useful?

7 Wrapper classes Can anyone think of any use for maybe creating our own wrapper- like classes? For example, when else might creating an instance of a class that stores one int vs. just storing the int as an int, be useful? Do we recall how primitive types and class objects are passed (via parameters) into methods?

8 Wrapper classes Can anyone think of any use for maybe creating our own wrapper- like classes? For example, when else might creating an instance of a class that stores one int vs. just storing the int as an int, be useful? Do we recall how primitive types and class objects are passed (via parameters) into methods? – Objects (including arrays) are passed by reference – Primitive types are passed by value – What are the implications of this?

9 Wrapper classes Can anyone think of any use for maybe creating our own wrapper- like classes? For example, when else might creating an instance of a class that stores one int vs. just storing the int as an int, be useful? Do we recall how primitive types and class objects are passed (via parameters) into methods? – A reference to an object (including arrays) is passed in to method – Whereas the value of a variable of a primitive type is passed in to a method – What are the implications of this?

10 Wrapper classes A reference to an object (including arrays) is passed in to method Whereas the value of a variable of a primitive type is passed in to a method – What are the implications of this? Variables of primitive types cannot have their value changed because when a variable of a primitive type is passed in to a method, that method makes a copy of the variable for use within the method and the one passed in is unchanged. Why can an object's values change within a method?

11 Wrapper classes A reference to an object (including arrays) is passed in to method Whereas the value of a variable of a primitive type is passed in to a method – What are the implications of this? Variables of primitive types cannot have their value changed because when a variable of a primitive type is passed in to a method, that method makes a copy of the variable for use within the method and the one passed in is unchanged. Why can an object's values change within a method? – Because the reference is passed into the method and the method uses the exact memory location of that reference to access/change that data. – When the method call is made with an object reference parameter, a copy of the reference is made, not a copy of the data in that object.

12 Wrapper classes So, if you want a value of a primitive type to possibly change within a method, wrap it up in one of your own wrapper classes and make your method take in an object of the wrapper class type (not the primitive type.) e.g. public class IntWrapper { private int theint; public IntWrapper(int theint) { this.theint = theint; } public void setInt(int theint) { this.theint = theint; } public int getInt() { return theint; }

13 Wrapper classes Then you could have a method that takes in an object reference of type IntWrapper instead of a plain old int. public void changeInt(IntWrapper i) { i.setInt(42); } Call it like: IntWrapper myint = new IntWrapper(3); changeInt(myint); All that just so a method can change the value of an int!!!!

14 Wrapper classes Could we use an object of type IntWrapper in an ArrayList?

15 Linked lists A linked list is a data structure where every node contains data and reference(s) to other node(s.)

16 Linked lists In a singly linked list every node contains data and one reference to another node. –e.g. class Node { public AnyType data; // can have more data than one public Node next; } A Linked List is maintained by keeping a reference to the head of the list. From the head, we are able to get at all the other nodes in the list by following the next reference.

17 Linked lists class Node { public AnyType data; // can have more data than one public Node next; // when constructing a new node, we set its data and // set the next reference to null public Node(AnyType d) { data = d; next = null; }

18 Linked lists Let me draw a representation of a node on the board and the connection of that node to other nodes. Recall that a reference holds an address and when we want to visualize what is happening, we draw an arrow from the reference to the data at the address stored in the reference.

19 Linked lists Node head; // this will be a reference to the head of the LL // make sure this is at a scope high enough to be // accessible for the life of the linked list. // elsewhere we can have: Node n = new Node(somedata); head = n; // sets n to be the head of the linked list Node newnode = new Node(somedata2); // to add a newnode to the beginning of the list: newnode.next = head; head = newnode;

20 Linked lists Node newnode = new Node(somedata2); // to add a newnode to the end of the list (assuming the list is // not empty, i.e. head != null: Node currnode; currnode = head; while (currnode != null) { savenode = currnode; currnode = currnode.next; } savenode.next = newnode;

21 Linked lists Let's figure out how to – Insert after a particular node – Delete a particular node – Let's assume that the node we're looking for is definitely in the linked list.

22 Linked lists Let's figure out how to – Insert after a particular node // insert newnode after findnode currnode = head; while (!((currnode.data).equals(findnode.data))) { currnode = currnode.next; } newnode.next = currnode.next; currnode.next = newnode;

23 Linked lists Let's figure out how to – Delete a node // delete findnode Node prevnode; Node currnode = head; while (!((currnode.data).equals(findnode.data))) { prevnode = currnode; currnode = currnode.next; } prevnode.next = currnode.next; currnode = null;

24 Linked lists Now let's reconsider these operations to take into account that the list might be empty (i.e. that head == null). Also, let's reconsider the possibility that if we are looking for some particular node that it may not be in the linked list.

25 Linked lists Other options – Storing link to last node (tail) – Doubly linked lists (and their operations.) – An idea to have a "dummy" head and tail makes adding new nodes easier because we won't have a special case that when the linked list is empty, head doesn't need to be reset.

26 Linked lists A doubly linked list is a data structure where every node contains – data – a reference to previous node – a reference to next node What do you think the value of the next node is for the last element of a linked list? What do you think the value of the previous node is for the first element of a linked list?

27 Linked lists Let's implement our deck of cards as a linked list of card nodes instead of as an array of cards. Note well: –A user of the Deck and Card class should not need to know how we decided to store the Deck of Cards. i.e. the implementation details should be hidden from the user of these classes. Deck was implemented as an array of Cards, now we're going to change the implementation to a linked list of Cards and last time we discussed the possibility of storing the Cards in an ArrayList.

28 Linked lists Compare a linked list's behaviors to that of arrays and ArrayLists. – Which are dynamic in length? – How about ease of operations in terms of: speed of execution and programmer ease – Consider these operations: Add Insert Delete


Download ppt "CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google