Lecture10AbstractData TypesandDynamicRepresentatio

Lecture10AbstractData TypesandDynamicRepresentatio - CMPT...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPT 126: Lecture 11 Collections: Abstract Data Types and Dynamic Representation (Chapt. 12) Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University October 30, 2007 1 Collections and Data Structures A data structure is any programming construct, either defined in the language or by a programmer, used to organize data into a format to facilitate access and processing. Eg.: arrays, linked lists, and stacks can all be considered data structures. A collection is an object that serves as a repository for other objects. They may be implemented in a variety of ways (which is why they are often referred to as abstract data types (ADTs), determined by the data structure. The role of a collection is generally to provide the following services: add remove, manage the contained elements CMPT 126: Collections, Lecture 11 2 array vs ArrayList We already saw the array, which is limited in that: once created, its size cannot be changed, making it difficult to add or remove new elements. it has no build-in utility methods (though the Array class in Java does provide a few) they are not very useful for more complex data structures, such as sets or trees (to be discussed). The Java ArrayList is implemented using an array, but is dynamic, that is, it can shrink and grow, and includes helpful support methods. Drawbacks of ArrayList Slightly less efficient than arrays because of the extra work required to keep track of their size and to shrink and grow. They use more memory than arrays. They reserve extra memory, at worst twice as much as an array storing the same number of elements, so adding can be done quickly. Unusual syntax. CMPT 126: Collections, Lecture 11 3 Using a collection or ADT If you want to read every line of a text file into memory, which data structure would you use? Problem: Write a complete method that takes a sorted array of ints and returns an array of ints that is identical to the input, except that it does not contain any duplicates. The size of the array should be equal to the number of int it contains. 2 , 2 , 4 , 5 , 6 , 7 , 8 , 8- 2 , 4 , 5 , 6 , 7 , 8 CMPT 126: Collections, Lecture 11 4 Solution using an array public int removeDuplicates(int ar) { int tmp = new int[ar.length]; tmp[0] = ar[0]; int next = 1; for (int i=1; i<ar.length; i++) { if (ar[i] != ar[i-1]) { tmp[next] = ar[i]; next++; } } //Create a new array of the correct size int result = new int[next]; for (int j=0; j<result.length; j++) result[j] = tmp[j]; //copy return result; } Required the creating of 2 new arrays: tmp and result . CMPT 126: Collections, Lecture 11 5 Solution Using ArrayList static ArrayList<Integer> removeDuplicates2(int ar) { ArrayList<Integer> result = new ArrayList<Integer>(); result.set(0, ar[0]); for (int i=1; i<ar.length; i++) { if (ar[i] != ar[i-1]) { result.add(ar[i]); } } return result; } and using HashSet ......
View Full Document

This note was uploaded on 12/07/2010 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 6

Lecture10AbstractData TypesandDynamicRepresentatio - CMPT...

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

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