06-CollectionConsiderations

06-CollectionConsiderations - 1 Chapter 6 Collection...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 6 Collection Considerations Data Structures via Java and JUnit Rick Mercer 2 Outline A Generic Collection with Class object 6.2 ADTs, Collection Classes, and Data Structures The Java Interface 3 Generic Collections Collection classes: a big part of C Sc 227 Next up: Implement one class that can store a collection of any type elements One class to store Strings, Songs, JukeboxAccounts, There are three approaches Use Java's Collection Framework (not yet) Use Object (next) Use Java 5 Generics (later) First learn about class Object 4 The Object class Java has a class named Object It communicates with the operating system to allocate memory at runtime Object is the superclass of all other classes All classes extend Object or a class that extends Object , or a class that extends a class that extends Object , or 5 EmptyClass inherits the methods of Object public class EmptyClass extends Object extends Object { // This class inherits Object's 11 methods } // Inherits 11 methods from Object EmptyClass one = new EmptyClass(); EmptyClass two = new EmptyClass(); System.out.println(one.toString toString ()); System.out.println(one.hashCode hashCode ()); System.out.println(one.getClass getClass ()); System.out.println(two.toString()); System.out.println(two.hashCode()); System.out.println(one.equals equals (two)); one = two; System.out.println(one.equals(two)); Output EmptyClass@ffb8f763-4655261 class EmptyClass EmptyClass@ffbcf763-4393117 false true 6 One way assignment Can assign any reference to an Object object Object obj1 = new String("a string"); Object obj2 = new Integer(123); System.out.println(obj1.toString()); System.out.println(obj2.toString()); Output a string 123 But not the other way compiletime error String str = obj1; // incompatible types ^ Type mismatch: cannot convert from Object to String 7 Tricking the compiler into believing obj1 is String Sometimes an explicit cast is needed Enclose the class name with what you know the class to be in parentheses ( String ) and place it before the reference to the Object object. str = (String) (String) obj1; A reference to an Object object 8 Object can store a reference to any Java object not int or double The Object class allows collections of any type Use Object rather than any one specific type public class ObjectCollection { private Object elements ; private int size ; public ObjectCollection() { elements = new Object[10]; size = 0; } public void add(Object elementToAdd) { elements [ size ] = elementToAdd; size ++; } public Object get( int index) { return elements [index]; } } Code demo 9 Object as a parameter and a return type add has an Object parameter get has an Object and return type This means that you add or retrieve references to any class of object (no primitives) This is possible because of inheritance can to assign a reference to Object All Java classes extend Object 10...
View Full Document

Page1 / 35

06-CollectionConsiderations - 1 Chapter 6 Collection...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online