COMP200 GENERICS. OOP using Java, from slides by Shayan Javed
|
|
- Catherine Carter
- 5 years ago
- Views:
Transcription
1 1 1 COMP200 GENERICS OOP using Java, from slides by Shayan Javed
2 2 ArrayList and Java Generics
3 3 Collection A container object that groups multiple objects
4 4 Collection A container object that groups multiple objects! Example: Arrays
5 5 Collection A container object that groups multiple objects! Example: Arrays Java provides multiple classes/interfaces in the Java Collection Framework
6 6 Collection A container object that groups multiple objects! Example: Arrays Java provides multiple classes/interfaces in the Java Collection Framework! Going to look at ArrayList
7 7 Arrays Hold several elements (primitives/objects) of the same type
8 8 Arrays Hold several elements (primitives/objects) of the same type Size = static.! Once set, cannot be changed
9 9 Arrays Advantages:! Easy to access elements directly
10 10 Arrays Advantages:! Easy to access elements directly! Easy to traverse
11 11 Arrays Advantages:! Easy to access elements directly! Easy to traverse Disadvantages:! Have to decide on size before creating
12 12 Arrays Advantages:! Easy to access elements directly! Easy to traverse Disadvantages:! Have to decide on size before creating! Inserting/removing elements is troublesome Shifting elements
13 13 ArrayList (java.util.arraylist) Holds several objects in order (like Arrays)
14 14 ArrayList (java.util.arraylist) Holds several objects in order (like Arrays) Unlike Arrays:! Size does not need to be specified at creation time
15 15 ArrayList (java.util.arraylist) Holds several objects in order (like Arrays) Unlike Arrays:! Size does not need to be specified at creation time! Grows in size as items added
16 16 ArrayList (java.util.arraylist) Holds several objects in order (like Arrays) Unlike Arrays:! Size does not need to be specified at creation time! Grows in size as items added Provides useful methods for manipulating the list
17 17 ArrayList (java.util.arraylist) Constructors:! new ArrayList( ) - no need to pass in size (10)
18 18 ArrayList (java.util.arraylist) Constructors:! new ArrayList( ) - no need to pass in size (10)! new ArrayList(int) - initial capacity
19 19 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list
20 20 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index
21 21 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements
22 22 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list
23 23 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index
24 24 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list
25 25 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list! Object remove(int index): Removes the object at the index
26 26 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list! Object remove(int index): Removes the object at the index! int size(): Returns the no. of elements in the list
27 27 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list! Object remove(int index): Removes the object at the index! int size(): Returns the no. of elements in the list! int indexof(object o): Returns index of first matching element
28 28 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list! Object remove(int index): Removes the object at the index! int size(): Returns the no. of elements in the list! int indexof(object o): Returns index of first matching element! Object set(int index, Object o): Sets the object at the index
29 29 ArrayList (java.util.arraylist) Methods:! boolean add(object o): Appends object at the end of the list! void add(int index, Object o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(object o): Returns true if object is in the list! Object get(int index): Returns object at the index! boolean remove(object o): Removes the object from the list! Object remove(int index): Removes the object at the index! int size(): Returns the no. of elements in the list! int indexof(object o): Returns index of first matching element! Object set(int index, Object o): Sets the object at the index! Object clone(): Returns a shallow copy
30 30 ArrayList Example import java.util.arraylist;
31 31 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); initially // no size specified
32 32 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); initially // no size specified numbers.add(new Integer(10));
33 33 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); initially // no size specified numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to Integer(3)); */ numbers.add(new
34 34 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); initially // no size specified numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to Integer(3)); */ numbers.add(new int sum = 0; // sum up all the numbers in the list int number = 0;
35 35 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { }
36 36 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required why? }
37 37 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required why? sum += number; // auto un-boxing: Integer converted to int }
38 38 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ numbers.add( A String ); // will compile! int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast required why? sum += number; // auto un-boxing: Integer converted to int }
39 39 ArrayList Example import java.util.arraylist; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: converted to numbers.add(new Integer(3)); */ numbers.add( A String ); // will compile! int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // run-time error when i = 2 sum += number; // auto un-boxing: Integer converted to int }
40 40 ArrayList How do we make sure we are dealing with objects of only one type?
41 41 ArrayList How do we make sure we are dealing with objects of only one type? Solution: Java Generics
42 42 Java Generics Generics is the capability to parameterize types
43 43 Java Generics Generics is the capability to parameterize types Can define class/interface/method with generic types compiler replaces with concrete types.
44 44 Java Generics Generics is the capability to parameterize types Can define class/interface/method with generic types compiler replaces with concrete types. Introduced in JDK 1.5
45 45 java.util.arraylist<e> Methods:! boolean add(e o): Appends object at the end of the list! void add(int index, E o): Inserts object at specified index! void clear(): Removes all elements! boolean contains(e o): Returns true if object is in the list! E get(int index): Returns object at the index! boolean remove(e o): Removes the object from the list! E remove(int index): Removes the object at the index! int size(): Returns the no. of elements in the list! int indexof(e o): Returns index of first matching element! E set(int index, E o): Sets the object at the index! E clone(): Returns a shallow copy
46 46 Java Generics Type of objects it can hold is specified at declaration
47 47 Java Generics Type of objects it can hold is specified at declaration // will only hold Integer objects ArrayList<Integer> numbers = new ArrayList<Integer>();
48 48 ArrayList Example import java.util.arraylist; ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: */ numbers.add( A String ); // WON T COMPILE! int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast NOT required why? sum += number; // auto un-boxing: Integer converted to int }
49 49 ArrayList Example import java.util.arraylist; ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(new Integer(10)); numbers.add(3); /* auto-boxing: */ numbers.add( A String ); // WON T COMPILE! int sum = 0; // sum up all the numbers in the list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = numbers.get(i); // cast NOT required why? sum += number; // auto un-boxing: Integer converted to int }
50 50 Java Generics More strictness
51 51 Java Generics More strictness Fewer errors
52 52 Generic Classes public class AClass<E> { // properties and methods can use the E type }
53 53 Generic Classes public class AClass<E> { // properties and methods can use the E type ArrayList<E> numbers = new ArrayList<E>(); }
54 54 Generic Methods public class GenericMethodDemo { public static void main(string[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York }; }
55 55 Generic Methods public class GenericMethodDemo { public static void main(string[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York }; } public static <E> void print(e[] list) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } }
56 56 Generic Methods public class GenericMethodDemo { public static void main(string[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York }; } GenericMethodDemo.<Integer>print(integers); GenericMethodDemo.<String>print(strings); public static <E> void print(e[] list) { for (int i = 0; i < list.length; i++) System.out.print(list[i] + " "); System.out.println(); } }
57 57 Generic Interfaces public interface AnInterface<E> { // properties and methods can use the E type } public void amethod(e object);
58 58 Generic Interfaces public interface Comparable<E> { public int compareto(e object); }
59 59 Generic Interfaces public interface Comparable<E> { public int compareto(e object); } public interface Comparator<E> { public int compare(e object1, E object2); }
60 60 Generic Interfaces public Book implements Comparable{ } // comparison based on price public int compareto(object o) { Book b = (Book)o; if (price > b.getprice()) return 1; else if (price < b.getprice()) return -1; else return 0; }
61 61 Generic Interfaces public Book implements Comparable{ // comparison based on price public int compareto(object o) { Book b = (Book)o; if (price > b.getprice()) return 1; else if (price < b.getprice()) return -1; else return 0; } } HOW WOULD YOU USE JAVA GENERICS?
62 62 Generic Interfaces public Book implements Comparable<Book>{ } // comparison based on price public int compareto(book b) { Book b = (Book)o; if (price > b.getprice()) return 1; else if (price < b.getprice()) return -1; else return 0; }
63 63 Generic Interfaces public Book implements Comparable<Book>{ } // comparison based on price public int compareto(book b) { Book b = (Book)o; // no more casting! if (price > b.getprice()) return 1; else if (price < b.getprice()) return -1; else return 0; }
64 64 Generic Interfaces public Book implements Comparable<Book>{ } // comparison based on price public int compareto(book b) { Book b = (Book)o; // no more casting! if (price > b.getprice()) return 1; else if (price < b.getprice()) return -1; else return 0; }
65 65 Generic Interfaces public class ReviewComparison implements Comparator { public int compare (Object o1, Object o2) { Book b1 = (Book)o1; Book b2 = (Book)o2; if (b1.getacr() > b2.getacr()) return 1; if (b1.getacr() < b2.getacr()) return -1; else return 0; } }
66 66 Generic Interfaces public class ReviewComparison implements Comparator<Book> { } public int compare (Book b1, Book b2) { Book b1 = (Book)o1; Book b2 = (Book)o2; if (b1.getacr() > b2.getacr()) return 1; if (b1.getacr() < b2.getacr()) return -1; else return 0; }
67 67 Generic Interfaces public class ReviewComparison implements Comparator<Book> { } public int compare (Book b1, Book b2) { Book b1 = (Book)o1; Book b2 = (Book)o2; if (b1.getacr() > b2.getacr()) return 1; if (b1.getacr() < b2.getacr()) return -1; else return 0; }
68 68 Java Generics - Summary Allows you to create a general template Classes/methods/interfaces Versatile and strict
69 69 Back to ArrayList... How does it work behind the scenes?
70 70 Back to ArrayList... How does it work behind the scenes? Elements are stored inside an array! private array
71 71 Back to ArrayList... How does it work behind the scenes? Elements are stored inside an array! private array Array has an initial capacity
72 72 Back to ArrayList... How does it work behind the scenes? Elements are stored inside an array! private array Array has an initial capacity! Empty constructor [new ArrayList()]: capacity 10
73 73 Back to ArrayList... How does it work behind the scenes? Elements are stored inside an array! private array Array has an initial capacity! Empty constructor [new ArrayList()]: capacity 10! Otherwise specify capacity [new ArrayList(capacity)]
74 74 ArrayList When number of elements exceeds capacity (the add/insert method):
75 75 ArrayList When number of elements exceeds capacity (the add/insert method):! Internal array replaced by new one
76 76 ArrayList When number of elements exceeds capacity (the add/insert method):! Internal array replaced by new one! Elements from the old array copied over
77 77 ArrayList When number of elements exceeds capacity (the add/insert method):! Internal array replaced by new one! Elements from the old array copied over Insert and remove require shifting of elements
78 78 ArrayList When number of elements exceeds capacity (the add/insert method):! Internal array replaced by new one! Elements from the old array copied over Insert and remove require shifting of elements! (Recall add/remove in Project1)
79 79 Efficiency of ArrayLists Reallocation of underlying array = when capacity reached and need to add more elements
80 80 Efficiency of ArrayLists Reallocation of underlying array = when capacity reached and need to add more elements Add/Insert/Remove = costly because requires shifting of elements in array
81 81 Efficiency of ArrayLists Reallocation of underlying array = when capacity reached and need to add more elements Add/Insert/Remove = costly because requires shifting of elements in array Accessing element (get) = fast just like an array
82 82 Efficiency of ArrayLists Reallocation of underlying array = when capacity reached and need to add more elements Add/Insert/Remove = costly because requires shifting of elements in array Accessing element (get) = fast just like an array Problems overcome with Linked Lists.
83 83 Other Java Collections LinkedList Stack Queue Will look at these in COMP201
Interfaces, collections and comparisons
תכנות מונחה עצמים תרגול מספר 4 Interfaces, collections and comparisons Interfaces Overview Overview O Interface defines behavior. Overview O Interface defines behavior. O The interface includes: O Name
More informationWeek 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5
CS2852 Exam 1 Name: No note-sheets, calculators, or other study aids on this exam. Write your initials at the top of each page except this one. Read through the whole exam before you get started. Have
More information11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures
Collections CSE 143 Java Collections Most programs need to store and access collections of data Collections are worth studying because... They are widely useful in programming They provide examples of
More information1. ArrayList and Iterator in Java
1. ArrayList and Iterator in Java Inserting elements between existing elements of an ArrayList or Vector is an inefficient operation- all element after the new one must be moved out of the way which could
More informationImplementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.
Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete
More informationBig O & ArrayList Fall 2018 Margaret Reid-Miller
Big O & ArrayList 15-121 Fall 2018 Margaret Reid-Miller Today Exam 1: Thursday, Oct 4, 2018 Exam 2 date: Currently Thur. Oct 25 th Move to Tues Oct 30 or Thur Nov 1? (Withdraw deadline Tues Nov 6.) Homework
More informationEXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction
More informationOutline. runtime of programs algorithm efficiency Big-O notation List interface Array lists
Outline runtime of programs algorithm efficiency Big-O notation List interface Array lists Runtime of Programs compare the following two program fragments: int result = 1; int result = 1; for (int i=2;
More informationJava Collections Framework: Interfaces
Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection Interface The List Interface The Iterator Interface The ListIterator
More informationImplementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.
Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete
More informationCS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)
CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15) A data structure is a software construct used to organize our data in a particular way. Some common data structures include lists,
More informationCONTAİNERS COLLECTİONS
CONTAİNERS Some programs create too many objects and deal with them. In such a program, it is not feasible to declare a separate variable to hold reference to each of these objects. The proper way of keeping
More informationCOURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE
COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE PREVIOUS COURSE CONTENT Inheritance Abstract classes Interfaces instanceof operator Nested classes Enumerations COUSE CONTENT Collections List Map Set Aggregate
More informationEXAMINATIONS 2016 TRIMESTER 2
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2016 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
More informationCSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists
CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists 1 A programming problem Consider the following task: Double values representing grades are read until the user enters a negative
More informationArray Based Lists. Collections
Array Based Lists Reading: RS Chapter 15 1 Collections Data structures stores elements in a manner that makes it easy for a client to work with the elements Specific collections are specialized for particular
More informationLists using ArrayList
Lists using ArrayList 1 ArrayList One of the drawbacks of arrays is that they do not make it easy to accommodate collections of arbitrary size. We have to commit ourselves to a fixed size when we introduce
More informationData Structures and Algorithms
Data Structures and Algorithms CS245-2015S-05 Abstract Data Types and Lists David Galles Department of Computer Science University of San Francisco 05-0: Abstract Data Types Recall that an Abstract Data
More informationCSE 143 Lecture 20. Circle
CSE 143 Lecture 20 Abstract classes Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; public double area() { return Math.PI * radius * radius; public
More informationCS S-05 Abstract Data Types and Lists 1
CS245-2016S-05 Abstract Data Types and Lists 1 05-0: Abstract Data Types Recall that an Abstract Data Type is a definition of a type based on the operations that can be performed on it. An ADT is an interface
More informationLecture 6: ArrayList Implementation
Lecture 6: ArrayList Implementation CS 62 Fall 2018 Alexandra Papoutsaki & William Devanny 1 Programming Assignment Weak AI/Natural Language Processing: Generate text by building frequency lists based
More informationJava Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering
Readings and References Java Collections "Collections", Java tutorial http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Spring 2004 Software Engineering http://www.cs.washington.edu/education/courses/403/04sp/
More informationEXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 2 COMP103 INTRODUCTION
More informationWhat is the Java Collections Framework?
1 of 13 What is the Java Collections Framework? To begin with, what is a collection?. I have a collection of comic books. In that collection, I have Tarzan comics, Phantom comics, Superman comics and several
More informationEXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 Trimester 1, MID-TERM TEST COMP103 Introduction
More informationSlides are adapted from the originals available at
C H A P T E R 1 1! Arrays and ArrayLists Little boxes, on a hillside, little boxes made of ticky-tacky Little boxes, little boxes, little boxes, all the same There s a green one and a pink one and a blue
More informationEXAMINATIONS 2017 TRIMESTER 2
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2017 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
More informationGeneric classes & the Java Collections Framework. *Really* Reusable Code
Generic classes & the Java Collections Framework *Really* Reusable Code First, a bit of history Since Java version 5.0, Java has borrowed a page from C++ and offers a template mechanism, allowing programmers
More information01. Which of the following statement describes dynamic resizing as is applies to the ArrayList class?
Exposure Java Chapter 11 Multiple Choice Test ArrayList Class DO NOT WRITE ON THIS TEST This test includes program segments, which are not complete programs. Answer such questions with the assumption that
More informationLesson 43.. ArrayList
Lesson 43.. ArrayList 43-1 You will recall from Lesson 42 the ArrayList is one of several classes that implement the List interface. As its name suggests, ArrayList also involves arrays. Basically, everything
More informationList ADT. B/W Confirming Pages
wu3399_ch8.qxd //7 :37 Page 98 8 List ADT O b j e c t i v e s After you have read and studied this chapter, you should be able to Describe the key features of the List ADT. the List ADT using an array
More informationModel Solutions. COMP 103: Mid-term Test. 19th of August, 2016
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes
More informationSTUDENT LESSON A15 ArrayList
STUDENT LESSON A15 ArrayList Java Curriculum for AP Computer Science, Student Lesson A15 1 STUDENT LESSON A15 - ArrayList INTRODUCTION: It is very common for a program to manipulate data that is kept in
More informationCSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,
CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding
More informationLinked Lists. Chapter 12.3 in Savitch
Linked Lists Chapter 12.3 in Savitch Preliminaries n Arrays are not always the optimal data structure: q An array has fixed size needs to be copied to expand its capacity q Adding in the middle of an array
More informationReview: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List
Review: Implementations CSE 143 Java ed s Reading: Ch. 23 The external interface is already defined Implementation goal: implement methods efficiently Array approach: use an array with extra space internally
More informationCS 307 Midterm 2 Fall 2008
Points off 1 2 3 4 5 Total off Net Score Exam Number: CS 307 Midterm 2 Fall 2008 Name UTEID login name TA's Name: Mikie Ron Sarah (Circle One) Instructions: 1. Please turn off your cell phones and other
More informationObject-Oriented Design and Programming (Java)
Object-Oriented Design and Programming (Java) Topics Covered Today 2.2 Collections 2.2.1 Arrays 2.2.2 Vectors and Iterators 2.2.3 Implementing the Collections of the Library System 2 What is a Collection?
More informationHere is how we use an arraylist. To access the arraylist code we can import the class via:
ArrayLists, Generics A data structure is a software construct used to organize our data in a particular way. Some common data structures include lists, stacks, queues, and heaps. Dynamic data structures
More informationJava Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering
Readings and References Java Collections References» "Collections", Java tutorial» http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Winter 2003 Software Engineering http://www.cs.washington.edu/education/courses/403/03wi/
More informationArray Lists. CSE 1310 Introduction to Computers and Programming University of Texas at Arlington. Last modified: 4/17/18
Array Lists CSE 1310 Introduction to Computers and Programming University of Texas at Arlington Last modified: 4/17/18 1 DEPARTAMENTAL FINAL EXAM Monday, DEC 10, 5:30pm-8pm rooms will be determined 2 Fixed
More informationTopic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1
Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing
More informationTest 6. moodle.yorku.ca CSE 1020
Test 6 When: Friday March 14, during the lab (14:30 16:00) Where: Lassonde building, labs 1006, 1004, 1002 Material: Chapter 1 8 of the textbook, with a focus on Chapter 8 What: One programming question
More informationUse of the ArrayList class
Use of the ArrayList class The ArrayList class is very similar to the Vector class. It also manages a collection of objects, and as the name indicates, does so with an array implementation. This is also
More informationLe L c e t c ur u e e 8 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Collections
Course Name: Advanced Java Lecture 8 Topics to be covered Collections Introduction A collection, sometimes called a container, is simply an object that groups multiple elements into a single unit. Collections
More informationBuilding Java Programs
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4, 16.4-16.5 2 A tree set Our SearchTree class is essentially a set. operations: add, remove, contains, size, isempty similar
More informationThe ArrayList class CSC 123 Fall 2018 Howard Rosenthal
The ArrayList class CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Describe the ArrayList class Discuss important methods of this class Describe how it can be used in modeling Much of the information
More informationThe Java Collections Framework and Lists in Java Parts 1 & 2
The Java Collections Framework and Lists in Java Parts 1 & 2 Chapter 9 Chapter 6 (6.1-6.2.2) CS 2334 University of Oklahoma Brian F. Veale Groups of Data Data are very important to Software Engineering
More informationLinked List. ape hen dog cat fox. tail. head. count 5
Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points
More informationCOMP200 INTERFACES. OOP using Java, from slides by Shayan Javed
1 1 COMP200 INTERFACES OOP using Java, from slides by Shayan Javed Interfaces 2 ANIMAL picture food sleep() roam() makenoise() eat() 3 ANIMAL picture food sleep() roam() makenoise() eat() 4 roam() FELINE
More information40) Class can be inherited and instantiated with the package 41) Can be accessible anywhere in the package and only up to sub classes outside the
Answers 1) B 2) C 3) A 4) D 5) Non-static members 6) Static members 7) Default 8) abstract 9) Local variables 10) Data type default value 11) Data type default value 12) No 13) No 14) Yes 15) No 16) No
More informationOverview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList
Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList This article discusses the main classes of Java Collection API. The following figure demonstrates the Java Collection framework. Figure
More informationDM550 / DM857 Introduction to Programming. Peter Schneider-Kamp
DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ ABSTRACT DATATYPES 2 Abstract Datatype (ADT)
More informationG51PRG: Introduction to Programming Second semester
G51PRG: Introduction to Programming Second semester Lecture 7 Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk Previous lecture es interfaces collections hierarchy in Java Lecture 7:
More informationCS 302: Introduction to Programming in Java. Lecture 12
CS 302: Introduction to Programming in Java Lecture 12 1 Review What is the 3-step processing for using Objects (think Scanner and Random)? Do objects use static methods or non-static (how do you know)?
More informationArrayList. Introduction. java.util.arraylist
ArrayList Introduction In this article from my free Java 8 course, I will be giving you a basic overview of the Java class java.util.arraylist. I will first explain the meaning of size and capacity of
More informationArrays and ArrayLists. David Greenstein Monta Vista High School
Arrays and ArrayLists David Greenstein Monta Vista High School Array An array is a block of consecutive memory locations that hold values of the same data type. Individual locations are called array s
More informationJava Collections. Wrapper classes. Wrapper classes
Java Collections Engi- 5895 Hafez Seliem Wrapper classes Provide a mechanism to wrap primitive values in an object so that the primitives can be included in activities reserved for objects, like as being
More informationJava Collections. Engi Hafez Seliem
Java Collections Engi- 5895 Hafez Seliem Wrapper classes Provide a mechanism to wrap primitive values in an object so that the primitives can be included in activities reserved for objects, like as being
More informationLinked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.
Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);
More informationAP Programming - Chapter 13 Lecture page 1 of 17
page 1 of 17 Arrays & ArrayList I. Simple vs. Structured Data Types A) Recall: Simple data types, such as integers and floats, cannot have an individual element broken down any further. Structured data
More informationFamily Name:... Other Names:... ID Number:... Signature... Model Solutions. COMP 103: Test 1. 9th August, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Model Solutions COMP 103: Test
More informationCOMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed
1 1 COMP200 ABSTRACT CLASSES OOP using Java, from slides by Shayan Javed Abstract Classes 2 3 From the previous lecture: public class GeometricObject { protected String Color; protected String name; protected
More information(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution
Ch 5 Arrays Multiple Choice 01. An array is a (A) (B) (C) (D) data structure with one, or more, elements of the same type. data structure with LIFO access. data structure, which allows transfer between
More informationboolean add(object o) Method Description (from docs API ) Method Description (from docs API )
Interface Collection Method Description (from docs API ) boolean add(object o) boolean addall(collection c) void clear() ensures that this collection contains the specified element adds all of the elements
More informationTaking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces
Taking Stock IE170: Algorithms in Systems Engineering: Lecture 7 Jeff Linderoth Department of Industrial and Systems Engineering Lehigh University January 29, 2007 Last Time Practice Some Sorting Algs
More informationExamination Questions Midterm 1
CS1102s Data Structures and Algorithms 10/2/2010 Examination Questions Midterm 1 This examination question booklet has 9 pages, including this cover page, and contains 15 questions. You have 40 minutes
More informationList ADT. Announcements. The List interface. Implementing the List ADT
Announcements Tutoring schedule revised Today s topic: ArrayList implementation Reading: Section 7.2 Break around 11:45am List ADT A list is defined as a finite ordered sequence of data items known as
More informationCOMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others
COMP-202 Generics Objects and Casting Generics Instantiation Usage ArrayList Iterators Iterator Lecture Outline 2 Objects and Casting All instances of all classes in Java are also of the class Object
More informationImplementing Lists, Stacks, Queues, and Priority Queues
Implementing Lists, Stacks, Queues, and Priority Queues CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 1 Objectives To design common features of lists in
More informationLesson 2.4 Arraylist
Lesson 24 Arraylist Mimi Duong Rosalba Rodriguez Java Crash Course January 6th, 2015 Data Structure ArrayLists Live Coding Methods Searching Through ArrayLists Classwork Storing Items in Java How have
More informationPrinciples of Computer Science
Principles of Computer Science Java Generics 19/09/2013 CSCI 2010 - Java Generics - F.Z. Qureshi 1 Topics Benefits of generics Using generic classes and interfaces Declaring generic classes and interfaces
More informationEECS 2011 M: Fundamentals of Data Structures
EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19 Iterators and
More informationCSC 321: Data Structures. Fall 2013
CSC 321: Data Structures Fall 2013 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList
More informationCS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12
CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can
More informationAlgorithms. Produced by. Eamonn de Leastar
Algorithms Produced by Eamonn de Leastar (edeleastar@wit.ie) Collections ± Collections Architecture ± Definition ± Architecture ± Interfaces ± Collection ± List ± Set ± Map ± Iterator ± Implementations
More informationCOMP103 Test. 5 Sept, 2007
Family Name:.......................... Other Names:.......................... ID Number:............................ COMP103 Test 5 Sept, 2007 Instructions Time: 90 minutes. Answer all the questions. There
More informationCSC 1052 Algorithms & Data Structures II: Lists
CSC 1052 Algorithms & Data Structures II: Lists Professor Henry Carter Spring 2018 Recap Collections hold and access elements based on content Order and index no longer considered Comparable elements implement
More informationCS 307 Final Spring 2008
Points off 1 2 3 4 5 Total off Net Score CS 307 Final Spring 2008 Name UTEID login name Instructions: 1. Please turn off your cell phones. 2. There are 5 questions on this test. 3. You have 3 hours to
More information(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution
Ch 5 Arrays Multiple Choice Test 01. An array is a ** (A) data structure with one, or more, elements of the same type. (B) data structure with LIFO access. (C) data structure, which allows transfer between
More informationCOMP-202: Foundations of Programming. Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015 Announcements Assignment 4 will be posted by the end of day today. Course Evaluations are now open.
More informationCS/CE 2336 Computer Science II
CS/CE 2336 Computer Science II UT D Session 11 OO Data Structures Lists, Stacks, Queues Adapted from D. Liang s Introduction to Java Programming, 8 th Ed. 2 What is a Data Structure? A collection of data
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 14 Array Wrap-Up Outline Problem: How can I store information in arrays without complicated array management? The Java language supports ArrayLists
More informationPrelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion
Prelim 1 CS 2110, October 1, 2015, 5:30 PM 0 1 2 3 4 5 Total Question Name True Short Testing Strings Recursion False Answer Max 1 20 36 16 15 12 100 Score Grader The exam is closed book and closed notes.
More informationCSC 1214: Object-Oriented Programming
CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:
More informationModel Solutions. COMP 103: Test April, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 40 minutes
More informationData abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003
Data abstractions: ADTs Invariants, Abstraction function Lecture 4: OOP, autumn 2003 Limits of procedural abstractions Isolate implementation from specification Dependency on the types of parameters representation
More informationCSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
CSE 143 Lecture 13: Interfaces, Comparable reading: 9.5-9.6, 16.4, 10.2 Related classes Consider classes for shapes with common features: Circle (defined by radius r ): area = r 2, perimeter = 2 r Rectangle
More informationCS 307 Midterm 2 Fall 2009
Points off 1 2 3 4 5 Total off Net Score Exam Number: CS 307 Midterm 2 Fall 2009 Name UTEID login name TA's Name: Oswaldo Rashid Swati (Circle One) Instructions: 1. Please turn off your cell phones and
More informationModel Solutions. COMP 103: Test May, 2013
Family Name:............................. Other Names:............................. ID Number:............................... Signature.................................. Instructions Time allowed: 45 minutes
More informationCOMP-202 Unit 7: More Advanced OOP. CONTENTS: ArrayList HashSet (Optional) HashMap (Optional)
COMP-202 Unit 7: More Advanced OOP CONTENTS: ArrayList HashSet (Optional) HashMap (Optional) Managing a big project Many times, you will need to use an Object type that someone else has created. For example,
More informationCSC 321: Data Structures. Fall 2012
CSC 321: Data Structures Fall 2012 Lists, stacks & queues Collection classes: List (ArrayList, LinkedList), Set (TreeSet, HashSet), Map (TreeMap, HashMap) ArrayList performance and implementation LinkedList
More informationJava Language Features
Java Language Features References: Object-Oriented Development Using Java, Xiaoping Jia Internet Course notes by E.Burris Computing Fundamentals with Java, by Rick Mercer Beginning Java Objects - From
More informationQuestion 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.
CSE 143 Sp04 Midterm 2 Page 1 of 10 Reference information about some standard Java library classes appears on the last pages of the test. You can tear off these pages for easier reference during the exam
More informationGenerics Collection Framework
Generics Collection Framework Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad Website: www.sisoft.in Email:info@sisoft.in Phone: +91-9999-283-283 Generics
More informationAbstract Data Types and Data Structures
Unit 6, Part 1 Abstract Data Types and Data Structures Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Congrats on completing the first half! In the second half, we will study fundamental
More informationAn Introduction to Data Structures
An Introduction to Data Structures Advanced Programming ICOM 4015 Lecture 17 Reading: Java Concepts Chapter 20 Fall 2006 Adapded from Java Concepts Companion Slides 1 Chapter Goals To learn how to use
More informationGrouping Objects (I)
KTH ROYAL INSTITUTE OF TECHNOLOGY Stockholm Sweden Grouping Objects (I) Managing collections of objects Ric Glassey glassey@kth.se Main concepts to be covered Grouping Objects Using ArrayLists Looping
More informationLetterkenny Institute of Technology
Letterkenny Institute of Technology Course Code: OOPR K6A02 BACHELOR OF SCIENCE IN COMPUTING WITH BUSINESS APPLICATIONS MULTIMEDIA AND DIGITAL ENTERTAINMENT COMPUTER SECURITY & DIGITAL FORENSICS COMPUTER
More informationCOSC 123 Computer Creativity. Java Lists and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan
COSC 123 Computer Creativity Java Lists and Arrays Dr. Ramon Lawrence University of British Columbia Okanagan ramon.lawrence@ubc.ca Objectives 1) Create and use arrays of base types and objects. 2) Create
More information