lec7 - Lecture 7 Data structure and algorithm libraries A...

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

View Full Document Right Arrow Icon
Page 1 of 36 CSE 100, UCSD: LEC 7 Lecture 7 Data structure and algorithm libraries A tour of the Java Collections Framework Iterators Iterators for lists and trees Reading: Weiss Ch. 1, online Javadoc documentation for the Collections API
Background image of page 1

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

View Full Document Right Arrow Icon
Page 2 of 36 CSE 100, UCSD: LEC 7 Learning and using data structures Learning data structures has two parts: learning how to design and implement data structures learning how to use data structures In language environments like ANSI C, Pascal, etc., very few data structures are available by default (maybe just arrays and records); everything else you have to write yourself However in other language environments such as Smalltalk, C++, Java 2, very nice standard data structure libraries are available For many applications, it makes sense to use these if possible: they are well-designed, well-written, well-tested, and are available wherever the standard environment for the language is available We will look at the Standard Template Library for C++ (later) and and the Collections framework for Java 5 (now)
Background image of page 2
Page 3 of 36 CSE 100, UCSD: LEC 7 Generic data structures in Java version 1.2 (aka “Java 2”) Java versions 1.2 and later provide a number of extensions and enhancements over version 1.1 One of these is a high-level data structure library called the Java Collections Framework Then in version 1.5, generics were added to the Java language, and in particular, incorporated into the Collections Framework The main aspects of the Collections Framework are shown on the next pages; for the most part these things live in the java.util package The Collections Framework provides a well-designed datastructures Application Programming Interface (API) that is worth studying!
Background image of page 3

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

View Full Document Right Arrow Icon
Page 4 of 36 CSE 100, UCSD: LEC 7 Toward Java generics In Java prior to 1.5 (a.k.a. Java 5), a “generic” data structure was just one that was designed to hold Objects Since in Java every object is-a Object by inheritance, this is a powerful and general approach For example, you can create a Vector, and put any kind of object in it. ..e.g. Integers or Strings (though not primitive types); Vector v = new Vector(); v.add(new Integer(3)); v.add(new Integer(333)); v.add(new String("xyz")); v.add(777); // compile time error Of course, when you take an object out of the Vector, you need to downcast in order to access the specific aspects of its true type: int i = ( (Integer) v.get(0) ) . intValue(); The downcast can fail at runtime with a ClassCastException if you do not correctly keep track of the type of object stored: String s = (String) v.get(1); // run time error
Background image of page 4
Page 5 of 36 CSE 100, UCSD: LEC 7 Java and type checking Java is typesafe : attempts to use an object in a way inconsistent with its type are detected as errors These errors can be detected at compile time , or at run time It is better to detect them at compile time! The compiler detects and localizes errors automatically, without having to run and
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

lec7 - Lecture 7 Data structure and algorithm libraries A...

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

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