Download presentation
Presentation is loading. Please wait.
Published byKory Cobb Modified over 9 years ago
2
Sadegh Aliakbary Sharif University of Technology Fall 2010
3
Agenda Containers Fall 2010Sharif University of Technology3
4
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
5
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
6
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
7
Pair class (Quiz) Pair equals toString Fall 2010Sharif University of Technology7
8
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
9
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
10
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
11
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
12
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
13
Fall 2010Sharif University of Technology13
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
Set A set is a list with no duplicate Suppose we want to implement such a class How?! Fall 2010Sharif University of Technology25
26
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
27
Quiz! Fall 2010Sharif University of Technology27
28
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.