Sadegh Aliakbary Sharif University of Technology Fall 2010.

Slides:



Advertisements
Similar presentations
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Information and Computer Sciences University of Hawaii, Manoa
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Peyman Dodangeh Sharif University of Technology Spring 2014.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Advanced Programming in Java
Advanced Programming in Java
Implementing ArrayList Part 1
Advanced Programming in Java
Advanced Programming Behnam Hatami Fall 2017.
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Advanced Programming in Java
CSE 143 Lecture 27: Advanced List Implementation
Advanced Programming in Java
ArrayLists 22-Feb-19.
Collections Framework
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists 27-Apr-19.
ArrayList.
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

Sadegh Aliakbary Sharif University of Technology Fall 2010

Agenda Containers Fall 2010Sharif University of Technology3

A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } class B extends A{ public Object f(Object o){ return new String("salam"); } B.f() overrides A.f() Fall 2010Sharif University of Technology4

A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } class B extends A{ public String f(Object o){ return new String("salam"); } B.f() overrides A.f() Fall 2010Sharif University of Technology5

A Note on Inheritance class A{ public Object f(Object o){ return new Object(); } } class B extends A{ public Object f(String o){ return new String("salam"); } } B.f() is overloading A.f() B.f() does not override A.f() Fall 2010Sharif University of Technology6

Pair class (Quiz) Pair equals toString Fall 2010Sharif University of Technology7

class Pair { private T first; private K second; public Pair(T t, K k) { this.first = t; this.second = k; } public T getFirst() { return first; } public K getSecond() { return second; } public String toString() { return "[" + second + ", " + first + "]"; } Fall 2010Sharif University of Technology8

Pair pair1 = new Pair (4, "Ali"); Integer i = pair1.getFirst(); String s = pair1.getSecond(); Pair pair2 = new Pair ("salam", true); String ss = pair2.getFirst(); Boolean bb = pair2.getSecond(); Fall 2010Sharif University of Technology9

equals() method public boolean equals(Pair pair) { return pair.first.equals(first) && pair.second.equals(second); } What is wrong with this implementation? Fall 2010Sharif University of Technology10

boolean equals(Pair pair) It should check for nullity of pair It should check for nullity of pair.first and pair.second It should check for nullity of this.first and this.second This method does not override equals() It is overloading it Correct signature: boolean equals(Object pair) What if parameter is not a Pair? Fall 2010Sharif University of Technology11

Type Checking public boolean equals(Object o) { Pair pair = null; try{ pair = (Pair ) o; }catch(ClassCastException e){ return false; } return pair.first.equals(first) && pair.second.equals(second); } Fall 2010Sharif University of Technology12

Fall 2010Sharif University of Technology13

Array Suppose we have an array of students Student[] students = new Student[34]; What if we do not know the array size? A default initial size What if we want to add more students to array? Double the size of array Copy old elements What if we want to remove some students from array? Nullify elements What about the array size? An sparse array We need a dynamic array Fall 2010Sharif University of Technology15

Imagine if arrays was sth like: Student[] students = new Student[0]; students.add(new Student("Ali Alavi")); students.add(new Student("Taghi Taghavi")); System.out.println(students[1]); students.remove(0); But arrays are not so cute! Fall 2010Sharif University of Technology16

ArrayList Java introduces Collection classes for this purpose ArrayList students = new ArrayList(); students.add(new Student("Ali Alavi")); students.add(new Student("Taghi Taghavi")); students.remove(0); Fall 2010Sharif University of Technology17

Generic ArrayList ArrayList is also a generic type ArrayList students = new ArrayList (); students.add(new Student("Ali Alavi")); students.add(new Student("Taghi Taghavi")); students.remove(0); students.remove(new Student("Ali Alavi")); Student student = students.get(0); System.out.println(student); ArrayList implements generic interface List Fall 2010Sharif University of Technology18

interface List { int size(); boolean isEmpty(); boolean contains(Object o); boolean add(E e); boolean remove(Object o); void clear(); E get(int index); E set(int index, E element); void add(int index, E element); E remove(int index); int indexOf(Object o); int lastIndexOf(Object o); List subList(int fromIndex, int toIndex); } Fall 2010Sharif University of Technology19

ArrayList list = new ArrayList (); Scanner scanner = new Scanner(System.in); while(true){ String input = scanner.next(); if(input.equalsIgnoreCase("exit")) break; list.add(input); } if(list.isEmpty()){ System.out.println("No string entered"); }else{ System.out.println("" + list.size() + " strings enetered"); if(list.contains("Ali")) System.out.println("Ali Found!"); for (String s : list) { System.out.println(s); } Fall 2010Sharif University of Technology20

ArrayList or Array? That is the question Do we need a dynamic array? Add Remove Performance issue Array : CPU instructions Fall 2010Sharif University of Technology21

Array to List Guess how? String[] strings = {"ali", "taghi"}; ArrayList list = new ArrayList (); for (String string : strings) { list.add(string); } Fall 2010Sharif University of Technology22

List to Array Two methods: Object[] toArray(); T[] toArray(T[] a); ArrayList list = new ArrayList (); Object[] array = list.toArray(); String[] array2 = list.toArray(new String[list.size()]); Fall 2010Sharif University of Technology23

Tell Me… Why toArray() returns Object[]? True/False ArrayList is subclass of List ArrayList is subclass of ArrayList ArrayList is subclass of List Fall 2010Sharif University of Technology24

Set A set is a list with no duplicate Suppose we want to implement such a class How?! Fall 2010Sharif University of Technology25

Set Implementation class Set extends ArrayList { public boolean add(E e) { if(!contains(e)) return super.add(e); return false; }; public boolean add(int index, E e) {...} } Fall 2010Sharif University of Technology26

Quiz! Fall 2010Sharif University of Technology27

Quiz A table is a List of Pairs We want a table for storing students information For each student we store Student ID Student Name Write a method which removes a student from a table This method has the table as a parameter It also has studentID as parameter Use this method in a piece of code Fall 2010Sharif University of Technology28