Array List and Hash Set, Slides of Object Oriented Programming

Lecture from Object Oriented Programming and Data Structures course with following key points: Array List and Hash Set, Generic Programming, Simplifies Programming, Strings, Vector, Interface Collection, Parsing Arithmetic Expressions

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(9)

104 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Generics with ArrayList and HashSet
ge·ner·ic adjective \ˈnerik, -rēk\
relating or applied to or descriptive of all members of a genus,
species, class, or group: common to or characteristic of a whole
group or class: typifying or subsuming: not specific or individual.
From Wikipedia: generic programming: a style of computer
programming in which algorithms are written in terms of to-be-
specified-later types that are then instantiated when needed for
specific types provided as parameters.
In Java:Without generics, every Vector object contains a list of
elements of class Object. Clumsy
With generics, we can have a Vector of Strings, a Vector of
Integers, a Vector of Genes. Simplifies programming, guards
against some errors
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Array List and Hash Set and more Slides Object Oriented Programming in PDF only on Docsity!

Generics with ArrayList and HashSet

ge·ner·ic adjective \jə̇ ˈnerik, - rēk
relating or applied to or descriptive of all members of a genus, species, class, or group: common to or characteristic of a whole group or class: typifying or subsuming: not specific or individual. From Wikipedia: generic programming: a style of computer programming in which algorithms are written in terms of to-be- specified-later types that are then instantiated when needed for specific types provided as parameters. In Java: Without generics, every Vector object contains a list of elements of class Object. Clumsy With generics, we can have a Vector of Strings, a Vector of Integers, a Vector of Genes. Simplifies programming, guards against some errors

Generics and Java’s Collection Classes

ge·ner·ic adjective \jə̇ ˈnerik, - rēk
relating or applied to or descriptive of all members of a genus, species, class, or group: common to or characteristic of a whole group or class: typifying or subsuming: not specific or individual. From Wikipedia: generic programming: a style of computer programming in which algorithms are written in terms of to-be- specified-later types that are then instantiated when needed for specific types provided as parameters. In Java: Without generics, every Vector object contains a list of elements of class Object. Clumsy With generics, we can have a Vector of Strings, a Vector of Integers, a Vector of Genes. Simplifies programming, guards against some errors

Generics with ArrayList and HashSet

HashSet s= new HashSet(); HashSet@y 2 Hashset Object Fields that contain a setof objects {o 0 , o 1 , …, o size()- 1

HashSet() add(Object) contains(Object) size() remove(Object) … s HashSet@y 2 HashSet An object of class HashSet contains a growable/shrinkable set of elements (of class Object). You can get the size of the set, add an object to the set, remove an object, etc. More methods exist! Look at them! Don’t ask what “hash” means. Just know that a Hash Set object maintains a set

Iterating over a HashSet or ArrayList

HashSet s= new HashSet(); … code to store values in the set … for (Object e : s) { System.out.println(c); } HashSet@y 2 HashSet Object Fields that contain a setof objects {o 0 , o 1 , …, o size()- 1

HashSet() add(Object) contains(Object) size() remove(Object) … s HashSet@y 2 HashSet A loop whose body is executed once with e being each element of the set. Don’t know order in which set elements processed Use same sort of loop to process elements of an ArrayList in the order in which they are in the ArrayList.

Generics: say we want Vector of ArrayList only

API specs: ArrayList declared like this: public class ArrayList extends AbstractList implements List … { … } Means: Can create Vector specialized to certain class of objects: vs.add(3); vi.add(“abc”); These are illegal int n= vs.get( 0 ).size(); vs.get( 0 ) has type String No need to cast ArrayList vs= new ArrayList (); //only Strings ArrayList vi= new ArrayList (); //only Integers

Generics allow us to say we want Vector of Strings only

API specs: Vector declared like this: public class Vector extends AbstractList implements List … { … } Full understanding of generics is not given in this recitation. E.g. We do not show you how to write a generic class. Important point : When you want to use a class that is defined like Vector above, you can write Vector v= new Vector(…); to have v contain a Vector object whose elements HAVE to be of class C, and when retrieving an element from v, its class is C.

Interface Collection: abstract methods for dealing with a group of objects (e.g. sets, lists) Abstract class AbstractCollection: overrides some abstract methods with methods to make it easier to fully implement Collection AbstractList, AbstractQueue, AbstractSet, AbstractDeque overrides some abstract methods of AbstractCollection with real methods to make it easier to fully implement lists, queues, set, and deques Next slide contains classes that you should become familiar with and use. Spend time looking at their specifications. There are also other useful Collection classes

ArrayList extends AbstractList: An object is a growable/shrinkable list of values implemented in an array Arrays: Has lots of static methods for dealing with arrays —searching, sorting, copying, etc. HashSet extends AbstractSet: An object maintains a growable/shrinkable set of values using a technique called hashing. We will learn about hashing later. LinkedList extends AbstractSequentialList: An object maintains a list as a doubly linked list Stack extends Vector: An object maintains LIFO (last-in- first-out) stack of objects Vector extends AbstractList: An object is a growable/shrinkable list of values implemented in an array. An old class from early Java

Vector Format of ArrayList object

AbstractList AbstractCollection Object List Collection Iterable List Collection Iterable Collection Iterable Iterable Not discussed today Interface List: abstract methods for dealing with a list of objects (o 0 , …, on- 1 ). Examples: arrays, Vectors Abstract class AbstractList: overrides some abstract methods with real methods to make it easier to fully implement List Homework: Look at API specifications and build diagram giving format of HashSet

Parsing Arithmetic Expressions

We show you a real grammar for arithmetic expressions with integer operands; operations +, - , *, /; and parentheses ( ). It gives precedence to multiplicative operations. We write a recursive descent parser for the grammar and have it generate instructions for a stack machine (explained later). You learn about infix, postfix, and prefix expressions. Introduced in lecture briefly, to show use of grammars and recursion. Done more thoroughly and carefully here. Historical note: Gries wrote the first text on compiler writing, in

  1. It was the first text written/printed on computer, using a simple formatting application. It was typed on punch cards. You can see the cards in the Stanford museum; visit infolab.stanford.edu/pub/voy/museum/pictures/display/floor 5 .htm

Infix requires parentheses. Postfix doesn’t

(5 + 6) * (4 – 3) Infix 5 6 + 4 3 - * Postfix 5 + 6 * 3 Infix 5 6 3 * + Postfix Math convention: * has precedence over +. This convention removes need for many parentheses Task: Write a parser for conventional arithmetic expressions whose operands are ints.

  1. Need a grammar for expressions, which defines legal arith exps, giving precedence to * / over + -
  2. Write recursive procedures, based on grammar, to parse the expression given in a String. Called a recursive descent parser

Use 3 syntactic categories: , , Grammar

A has one of 3 forms:

  1. integer
  2. ( ) Show “syntax trees” for 3 – – 5 – ( 3 + 2 ) 3
  • 5

3 + 2 ( )

Haven’t shown grammar yet ::= int | | ( )

Use 3 syntactic categories: , , Grammar

A is: followed by 0 or more occurrences of addop where addop is + or - 3 + ( 5 + 2 ) _ 6 ::= { { + | - } 1 }

Initialized to a String that contains an arithmetic expression. Delivers the tokens in the String, one at a time

Class Scanner

Expression : 3445 *( 20 + 16 ) Tokens: 3445

( 20

16 ) All parsers use a scanner, so they do not have to deal with the input character by character and do not have to deal with whitespace