Interfaces, collections and comparisons
|
|
- Marion Collins
- 6 years ago
- Views:
Transcription
1 תכנות מונחה עצמים תרגול מספר 4 Interfaces, collections and comparisons
2 Interfaces
3 Overview
4 Overview O Interface defines behavior.
5 Overview O Interface defines behavior. O The interface includes: O Name O Functionality (methods).
6 Overview O Interface defines behavior. O The interface includes: O Name O Functionality (methods). O Classes can implement one or more interfaces.
7 Importance
8 Importance O Interfaces are useful for the following: O Capturing similarities among unrelated classes without artificially forcing a class relationship. O Declaring methods that one or more classes are expected to implement. O Revealing an object's programming interface without revealing its class.
9 Syntax O Syntax of defining java interface is: interface <interface-name>{ //method signatures Interface of entities that can talk: interface Talkable { void sayhello(); void saybye();
10 Talkable example
11 Talkable example public class Cat implements Talkable{
12 Talkable example public class Cat implements Talkable{ public class Dog implements Talkable{
13 Talkable example public class Cat implements Talkable{ public class Dog implements Talkable{ Note that each of the animals can have it own methods
14 Talkable example public class Cat implements Talkable{ public void sayhello(){ public class Dog implements Talkable{ Note that each of the animals can have it own methods
15 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public class Dog implements Talkable{ Note that each of the animals can have it own methods
16 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ public class Dog implements Talkable{ Note that each of the animals can have it own methods
17 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public class Dog implements Talkable{ Note that each of the animals can have it own methods
18 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){ public class Dog implements Talkable{ Note that each of the animals can have it own methods
19 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ Note that each of the animals can have it own methods
20 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ Note that each of the animals can have it own methods
21 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ Note that each of the animals can have it own methods
22 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ Note that each of the animals can have it own methods
23 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); Note that each of the animals can have it own methods
24 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ Note that each of the animals can have it own methods
25 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ System.out.println( Hou Hou!"); Note that each of the animals can have it own methods
26 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ System.out.println( Hou Hou!"); public void run(){ Note that each of the animals can have it own methods
27 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ System.out.println( Hou Hou!"); public void run(){... Note that each of the animals can have it own methods
28 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ System.out.println( Hou Hou!"); public void run(){... Note that each of the animals can have it own methods
29 Talkable example public class Cat implements Talkable{ public void sayhello(){ System.out.println("Miau!"); public void saybye(){ System.out.println("Miau!"); public void rest(){... public class Dog implements Talkable{ public void sayhello(){ System.out.println( Hav Hav!"); public void saybye(){ System.out.println( Hou Hou!"); public void run(){... Note that each of the animals can have it own methods
30 What is a Dog (Cat)? O Dog is a Talkable, and also a Dog O Cat is a Talkable, and also a Cat O Every dog is a Talkable but not every Talkable is a Dog!!!
31 Talkable example O At this step, after we defined our different animals we want each of them to say hello public class MainFile { public static void main(string[] args) { Talkable t1=new Dog(); Talkable t2=new Cat(); System.out.print("The dog says Hi:"); t1.sayhello(); System.out.print("The cat says Hi:"); t2.sayhello(); Runtime decision (Talkable vs. dog\cat)
32 Talkable example public class RuntimeTalkable{ public static void main(string[] args) { Talkable t1; if (args[0].equals( Dog )) { t1=new Dog(); else { t1=new Cat(); System.out.print( A random animal says Hi:"); t1.sayhello(); Runtime decision (Talkable vs. dog\cat)
33 Using functions private static void makeitsayhi(talkable t1) { t1.sayhello(); The function gets a Talkable and the output is based on the specific class that t1 belongs to.
34 Collections
35 Collections
36 Collections O Everything in Java is an object.
37 Collections O Everything in Java is an object. O We want to store similar objects together.
38 Collections O Everything in Java is an object. O We want to store similar objects together. O Collections - a group of single items O List stores the objects in a certain order. O Set the same object cannot appear more than once.
39 Collections O Everything in Java is an object. O We want to store similar objects together. O Collections - a group of single items O List stores the objects in a certain order. O Set the same object cannot appear more than once. O Each collection has it own advantages and disadvantages.
40 Collections O Everything in Java is an object. O We want to store similar objects together. O Collections - a group of single items O List stores the objects in a certain order. O Set the same object cannot appear more than once. O Each collection has it own advantages and disadvantages. O Have to choose the collection based on the program s requirements.
41 Collections Collection Set List TreeSet HashSet LinkedList ArrayList Collection, Set and List are interfaces
42 List
43 Overview O List is an interface for an ordered collection, may contain duplicate elements. O Supports Positional access: O methods such as get, set, add, addall, and remove. O Support Search O methods such as indexof and lastindexof.
44 Javadoc Available at: List - JAVADOC
45 Built-in functions O void add(int index, Object element) Inserts the specified element at the specified position index in this list. O boolean add(object o) Appends the specified element to the end of this list. O boolean addall(collection c) Appends all of the elements in the specified collection to the end of this list. O void clear() Removes all of the elements from this list.
46 Built-in functions
47 Built-in functions O Object get(int index) Returns the element at the specified position in this list.
48 Built-in functions O Object get(int index) Returns the element at the specified position in this list. O int indexof(object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
49 Built-in functions O Object get(int index) Returns the element at the specified position in this list. O int indexof(object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. O Object remove(int index) Removes the element at the specified position in this list.
50 Built-in functions O Object get(int index) Returns the element at the specified position in this list. O int indexof(object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. O Object remove(int index) Removes the element at the specified position in this list. O Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element.
51 Built-in functions O Object get(int index) Returns the element at the specified position in this list. O int indexof(object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. O Object remove(int index) Removes the element at the specified position in this list. O Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element. O int size() Returns the number of elements in this list.
52 Implementation O Useful lists: O ArrayList based on an array: O Fast random access. O Slow addition and removal from the middle of the list. O LinkedList based on a two-sided linked list O Slow random access. O Fast addition and removal from the middle of the list.
53 ArrayList - Overview O The ArrayList class implements the List interface. O ArrayList supports dynamic arrays that can grow and shrink as needed.
54 Time complexity Function get(int index) ArrayList O(1) LinkedList O(n) add(e element) O(1) O(1) add(int index, E element) O(n) O(n) remove(int index) O(n) O(n)
55 Time complexity 000,100 items
56 The talkable example public static void main(string[] args) { Talkable t1=new Dog(); Talkable t2=new Cat(); System.out.print("The dog says Hi:"); t1.sayhello(); System.out.print("The cat says Hi:"); t2.sayhello(); System.out.print("The dog says Bye:"); t1.saybye();
57 The talkable example O We want a list of talk able animals: public static void main(string[] args) { List l1 = new ArrayList (); List l2 = new LinkedList (); System.out.println("Using ArrayList"); l1.add(t1); l1.add(t2); System.out.println("Using LinkedList"); l2.add(t1); l2.add(t2);
58 Make them talk O Remember our list of talkable animals? O Now, lets make them make some sounds for (int i = 0; i < l1.size(); i++) { Talkable t=(talkable)l1.get(i); t.sayhello(); We iterated over each object in l1 (the ArrayList) or l2 (the LinkedList) and ask each of them to execute its implementation of sayhello.
59 Shortcut for (int i = 0; i < l1.size(); i++) { Talkable t=(talkable)l1.get(i); t.saybye(); for (Object entity: l1) { ((Talkable) entity).saybye();
60 Comparator
61 Comparator
62 Comparator O It will be really nice to sort our list.
63 Comparator O It will be really nice to sort our list. O Do we know what comes before what?
64 Comparator O It will be really nice to sort our list. O Do we know what comes before what? O How do we sort animals? O By Name O By Size
65 Comparator
66 Comparator O Do we have to define two implementations to sort a list from bottom to top and vice versa?
67 Comparator O Do we have to define two implementations to sort a list from bottom to top and vice versa? No, we can use Strategy pattern.
68 Comparator O Do we have to define two implementations to sort a list from bottom to top and vice versa? No, we can use Strategy pattern. O Based on this pattern, the sorting algorithm will use the external decision regarding how to sort the list.
69 Comparator O Do we have to define two implementations to sort a list from bottom to top and vice versa? No, we can use Strategy pattern. O Based on this pattern, the sorting algorithm will use the external decision regarding how to sort the list. O In order to affect the sorting method with collections we use the comparator.
70 Comparator \ Comparable O Any class can implement the above interfaces: interface Comparator { int compare(object o1, Object o2); interface Comparable { int compareto(object o) ; O Based on the implementation of these interfaces, the collections will decide how to sort the elements.
71 Comparable \ Comparator Parameter Comparable Comparator Sorting logic Implementati on Sorting method Calling method In same class whose objects are being sorted. Hence this is called natural ordering of objects Class whose objects to be sorted must implement this interface. E.g Country class needs to implement comparable to sort collection of country objects by area int compareto(object o1) This method compares this object with o1 object and returns a integer. Its value has following meaning: 1. positive this object is greater than o1 2. zero this object equals to o1 3. negative this object is less than o1 Collections.sort(List) Here objects will be sorted on the basis of CompareTo method In a separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc. Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface as well. E.g.- CountrySortByAreaComparator class can implement Comparator interface to sort collection of country objects by area int compare(object o1,object o2) This method compares o1 and o2 objects and returns a integer. Its value has following meaning: 1. positive o1 is greater than o2 2. zero o1 equals to o2 3. negative o1 is less than o2 Collections.sort(List, Comparator) Here objects will be sorted on the basis of Compare method in Comparator Package Java.lang.Comparable Java.util.Comparator
72 Code examples O ComparableMain O ComparatorMain O CountrySortByIdComparator
73 What have we learned today? Using interface Collections Sorting Customized sorting
11-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 informationCOMP200 GENERICS. OOP using Java, from slides by Shayan Javed
1 1 COMP200 GENERICS OOP using Java, from slides by Shayan Javed 2 ArrayList and Java Generics 3 Collection A container object that groups multiple objects 4 Collection A container object that groups multiple
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 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 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 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 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 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 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 informationClass Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References
Reading Readings and References Class Libraries CSE 142, Summer 2002 Computer Programming 1 Other References» The Java tutorial» http://java.sun.com/docs/books/tutorial/ http://www.cs.washington.edu/education/courses/142/02su/
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 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 informationCollections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)
Lecture 15 Summary Collections Framework Iterable, Collections List, Set Map Collections class Comparable and Comparator 1 By the end of this lecture, you will be able to use different types of Collections
More informationLecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map
Lecture 15 Summary Collections Framework Iterable, Collections List, Set Map Collections class Comparable and Comparator 1 By the end of this lecture, you will be able to use different types of Collections
More informationAdvanced Java Concepts Unit 2: Linked Lists.
Advanced Java Concepts Unit 2: Linked Lists. The List interface defines the structure of a linear collection. Here are some of its methods. boolean add( E element ) Appends the element to the end of the
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 information9/16/2010 CS Ananda Gunawardena
CS 15-121 Ananda Gunawardena A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data,
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 informationCS Ananda Gunawardena
CS 15-121 Ananda Gunawardena A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve and manipulate data,
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 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 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. 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 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 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 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 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 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 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 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 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 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 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 informationBuilding Java Programs
Building Java Programs Chapter 16 References and linked nodes reading: 16.1 2 Value semantics value semantics: Behavior where values are copied when assigned, passed as parameters, or returned. All primitive
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 informationStudent Number: Legibly write your name and student number on this page. Legibly write your name on the back page of this exam.
CSC207H1 S 2015 Midterm Test Duration 50 minutes Aids allowed: none Last Name: Student Number: First Name: Do not turn this page until you have received the signal to start. (Please fill out the identification
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 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 informationProgrammieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel)
Programmieren II Collections Alexander Fraser fraser@cl.uni-heidelberg.de (Based on material from T. Bögel) May 28, 2014 1 / 46 Outline 1 Recap Paths and Files Exceptions 2 Collections Collection Interfaces
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 informationLists using LinkedList
Lists using LinkedList 1 LinkedList Apart from arrays and array lists, Java provides another class for handling lists, namely LinkedList. An instance of LinkedList is called a linked list. The constructors
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 information# $ $ A.A. 2006/07 Tutor: Daniele Tiles &== %% & '( ) * + $, $ $ -' %./, $ * $ $ 7 5 &7 2$ $ $ 3&4&&& 5 +8$ 9%:
! A.A. 2006/07 Tutor: Daniele Tiles daniele.tiles@studio.unibo.it # $ $ %% & '( ) * + $, $ $ -' %./, 01 5 6 $ * $ $ 7 5 &7 2$ $ $ 3&4&&& 5 +8$ 9%: 5 6;< &&$ $ &== " 1&:+$? &7$ $ @$ 1&&# &A1+ $ $ $, $,
More informationArrays organize each data element as sequential memory cells each accessed by an index. data data data data data data data data
1 JAVA PROGRAMMERS GUIDE LESSON 1 File: JGuiGuideL1.doc Date Started: July 10, 2000 Last Update: Jan 2, 2002 Status: proof DICTIONARIES, MAPS AND COLLECTIONS We have classes for Sets, Lists and Maps and
More informationJava Collections Framework
Java Collections Framework Introduction In this article from my free Java 8 course, you will be given a high-level introduction of the Java Collections Framework (JCF). The term Collection has several
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 informationBinghamton University. CS-140 Fall Problem Solving. Creating a class from scratch
Problem Solving Creating a class from scratch 1 Recipe for Writing a Class 1. Write the class boilerplate stuff 2. Declare Fields 3. Write Creator(s) 4. Write accessor methods 5. Write mutator methods
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 informationNAME: c. (true or false) The median is always stored at the root of a binary search tree.
EE 322C Spring 2009 (Chase) Exam 2: READ THIS FIRST. Please use the back side of each page for scratch paper. For some of the questions you may need to think quite a bit before you write down an answer.
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 informationCSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7
CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7 Question 1. (4 points) (a) If a precondition is not true when a method is called, two possible ways to detect and handle the situation are to use an assert
More informationJava Array List Interview Questions
Java Array List Interview Questions codespaghetti.com/arraylist-interview-questions/ Array List Java Array List Interview Questions, Algorithms and Array List Programs. Table of Contents: CHAPTER 1: Top
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 informationGarbage Collection (1)
Coming up: Today: Finish unit 6 (garbage collection) start ArrayList and other library objects Wednesday: Complete ArrayList, basics of error handling Friday complete error handling Next week: Recursion
More informationCIS 265 Exam 2 First Name Last Name
CIS 265 Exam 2 First Name Last Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Which of the data types below does not allow duplicates? 1)
More informationList... NOTE We use List as illustrative example of collections Study the others from textbook!
List... NOTE We use List as illustrative example of collections Study the others from textbook! 25 What is a List? A List is an ordered Collection (sometimes called a sequence) Lists may contain duplicate
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 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 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 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 informationCSE 143 Au03 Midterm 2 Page 1 of 7
CSE 143 Au03 Midterm 2 Page 1 of 7 Question 1. (4 points) (a) If a precondition is not true when a method is called, two possible ways to detect and handle the situation are to use an assert statement
More informationToday. Book-keeping. Exceptions. Subscribe to sipb-iap-java-students. Collections. Play with problem set 1
Today Book-keeping Exceptions Subscribe to sipb-iap-java-students Collections http://sipb.mit.edu/iap/java/ Play with problem set 1 No class Monday (MLK); happy Hunting Problem set 2 on Tuesday 1 2 So
More informationObject-Oriented Programming in the Java language
Object-Oriented Programming in the Java language Part 6. Collections(1/2): Lists. Yevhen Berkunskyi, NUoS eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Just before we start Generics Generics are a
More informationINTRODUCTION TO DATA AND PROCEDURE
INTRODUCTION TO DATA AND PROCEDURE In this book, our goal is to understand computation. In particular, we want to be able to take any computational problem and produce a technique for solving it that is
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 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 informationLecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.
Lecture 15 Summary Collections Framework Iterable, Collections, Set Map Collections class Comparable and Comparator By the end of this lecture, you will be able to use different types of Collections and
More informationVector (Java 2 Platform SE 5.0) Overview Package Class Use Tree Deprecated Index Help
Overview Package Class Use Tree Deprecated Index Help PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes SUMMARY: NESTED FIELD CONSTR METHOD DETAIL: FIELD CONSTR METHOD Página 1 de 30 Java TM 2 Platform
More informationREMINDER CS121/IS223. Agenda. A Recap on Arrays. Array of Cars for the Car Race? Arrays of Objects (i)
Collections Revisited Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors REMINDER There is no class
More informationCS121/IS223. Collections Revisited. Dr Olly Gotel
CS121/IS223 Collections Revisited Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223
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 informationInterfaces and Collections
Interfaces and Collections COMPSCI 2S03 Mikhail Andrenkov Department of Computing and Software McMaster University Week 9: November 14-18 Mikhail Andrenkov Interfaces and Collections 1 / 25 Outline 1 Interfaces
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 informationCollections, Maps and Generics
Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.
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 informationBinghamton University. CS-140 Fall Interfaces
Interfaces 1 Arrays vs. ArrayLists Similarities Indexed list of data elements of a specific type Allow you to determine the number of elements Allow you to read (get) or write (set) individual elements
More informationInf1-OP. Collections. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 6, School of Informatics
Inf1-OP Collections Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein School of Informatics March 6, 2017 Rigidity of arrays Length of array is fixed at creation time. Can
More informationRecap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1
COMP209 Object Oriented Programming Container Classes 3 Mark Hall List Functionality Types List Iterator Adapter design pattern Adapting a LinkedList to a Stack/Queue Map Functionality Hashing Performance
More information(a) Write the signature (visibility, name, parameters, types) of the method(s) required
1. (6 pts) Is the final comprehensive? 1 2. (6 pts) Java has interfaces Comparable and Comparator. As discussed in class, what is the main advantage of Comparator? 3. (6 pts) We can use a comparator in
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 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 informationTha Java Programming Language
Tha Java Programming Language Kozsik Tamás (2000-2001) kto@elte.hu http://www.elte.hu/~k to/ III. Arrays, collections and other baseclasses Some of the baseclasses Object String StringBuffer Integer, Double,...
More informationArrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
1 Arrays Arrays in Java an array is a container object that holds a fixed number of values of a single type the length of an array is established when the array is created 2 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
More informationClass 32: The Java Collections Framework
Introduction to Computation and Problem Solving Class 32: The Java Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward Goals To introduce you to the data structure classes that come
More informationCS S-08 Arrays and Midterm Review 1
CS112-2012S-08 Arrays and Midterm Review 1 08-0: Arrays ArrayLists are not part of Java proper 08-1: Arrays Library class Created using lower-level Java construct: Array Arrays are like a stripped-down
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 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 informationAPCS Semester #1 Final Exam Practice Problems
Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester
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 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 informationObject-Oriented Programming with Java
Object-Oriented Programming with Java Recitation No. 2 Oranit Dror The String Class Represents a character string (e.g. "Hi") Explicit constructor: String quote = "Hello World"; string literal All string
More informationMIT AITI Lecture 18 Collections - Part 1
MIT AITI 2004 - Lecture 18 Collections - Part 1 Collections API The package java.util is often called the "Collections API" Extremely useful classes that you must understand to be a competent Java programmer
More informationJAVA COLLECTION FRAMEWORK & SETS
JAVA COLLECTION FRAMEWORK & SETS Ch07.4-5 & Ch10.5 Presentation for use with the textbook 1. Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
More informationCOMP 202. Programming With Arrays
COMP 202 Programming With Arrays CONTENTS: Arrays, 2D Arrays, Multidimensional Arrays The Array List Variable Length parameter lists The Foreach Statement Thinking Like A Programmer: Designing for arrays
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 informationCSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides adapted from Marty Stepp and Hélène Martin http://www.cs.washington.edu/143/ Related classes Consider classes for
More informationBuilding Java Programs. Interfaces and Comparable reading: , 10.2, 16.4
Building Java Programs Interfaces and Comparable reading: 9.5-9.6, 10.2, 16.4 2 Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations
More informationGeneric Programming. *Really* reusable code
Generic Programming *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 to create data structures
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 information