Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Tirgul 5.

Similar presentations


Presentation on theme: "OOP Tirgul 5."— Presentation transcript:

1 OOP Tirgul 5

2 What We’ll Be Seeing Today
Java collections you’ll be using in Ex3: LinkedList<String> HashSet<String> TreeSet<String> Debugger basics

3 Java collections you’ll be using

4 Java collections you’ll be using
Java comes with a useful collection of classes representing data structures, called the Java Collections Framework. All these data structures allow you to specify which type of objects you want to hold in them, using a Java feature called Generics.

5 Java collections you’ll be using
You will use the following in Ex3: LinkedList<String> HashSet<String> TreeSet<String>

6 Java collections you’ll be using
Iterable Collection Queue Deque List AbstractList LinkedList Set SortedSet NavigableSet TreeSet AbstractSet HashSet

7 LinkedList

8 LinkedList You already know what is a linked list, and you already understand Generics. Say I want a linked list holding Strings: The type of the reference The type of objects the collection holds An empty Diamond Operator LinkedList<String> stringList = new LinkedList<>(); LinkedList<E> implements List<E>, Queue<E> and Deque<E> The type of the collection

9 LinkedList To add a word (to the end of the list):
Now, to get the first String in the list I can write: strList.add(“word”); String s = strList.getFirst(); No downcasting needed!

10 LinkedList To add a word:
LinkedList<String> stringList = new LinkedList<>(); strList.add(“first”); strList.add(“second”); strList.add(“third”); String s = strList.getFirst(); System.out.println(s); What will this print?

11 LinkedList You can use any of the methods in LinkedList’s API, replacing E with the type you chose for the list (String, in this case)

12 LinkedList This means you can’t add things that are not a String:
But that is why we also don’t have to downcast the objects the list returns. Compilation errors! strList.add(3); strList.add(new Dog(“Goofy”));

13 LinkedList Java’s LinkedList is a doubly-linked list implementation of the linked list data structure. This means several useful operations can be done in constant time: addFirst(E element), addLast(E element) getFirst(), getLast()

14 LinkedList – Useful methods
Additional operations: clear() – removes all elements contains(E element) – returns true/false get(int index) index(Object o) - index of first occurrence remove(int index), remove(Object o) size()

15 LinkedList To iterate over all the elements: strList.add(“first”);
strList.add(“second”); strList.add(“third”); for (String str: strList){ System.out.println(str); }

16 LinkedList To iterate over all the elements: strList.add(“first”);
strList.add(“second”); strList.add(“third”); Iterator<String> it = strList.iterator(); while (it.hasNext()){ System.out.println(it.next()); }

17 LinkedList To delete an element while iterating: strList.add(“first”);
strList.add(“second”); strList.add(“third”); Iterator<String> it = strList.iterator(); while (it.hasNext()){ if (it.next().equals(“second”)){ it.remove(); } Removes the last element returned by the iterator

18 HashSet

19 HashSet You already know what is a hash set – no duplicate elements, using a hash table. Again, you will use Java’s generic HashSet implementation to hold Strings. The type of the reference The type of objects the collection holds An empty Diamond Operator Set<String> str = new HashSet<>(); The type of the collection

20 HashSet To add a word: Set<String> strSet = new HashSet<>(); strSet.add(“first”); strSet.add(“second”); strSet.add(“third”); for (String str: strSet){ System.out.println(str); } What will be printed to screen? Order is not preserved!

21 HashSet Java’s HashSet is a set implementation that uses a hash table to perform some operations in coanstant time (on average). This means several useful operations can be done in constant time: add(E element) contains(Object o) remove(Object o)

22 HashSet – Useful methods
Additional operations: clear() – removes all elements isEmpty() – returns true/false size() addAll(Collection<? extends E> c) removeAll(Collection<? extends E> c) retainAll(Collection<? extends E> c)

23 HashSet Set<String> oneToFour = new HashSet<>();
oneToFour.add(“one”); oneToFour.add(“two”); oneToFour.add(“three”); oneToFour.add(“four”); Set<String> threeToFive= new HashSet<>(); threeToFive.add(“three”); threeToFive.add(“four”); threeToFive.add(“five”);

24 HashSet Set operations: oneToFour.addAll(threeToFive);
oneToFour.removeAll(threeToFive); oneToFour.retainAll(threeToFive); “one” “two” “three” “five” “four” Set<String> oneToFour;

25 TreeSet

26 TreeSet It’s a set – so no duplicates.
Elements are ordered using some natural ordering (integers, strings…) The type of the reference The type of objects the collection holds An empty Diamond Operator NavigableSet<String> str = new TreeSet<>(); The type of the collection

27 TreeSet Java’s TreeSet is a set implementation that uses a tree as a data structure. This means several useful operations can be done in O(log n) time: add(E element) contains(Object o) remove(Object o)

28 TreeSet – Useful methods
The usual additional operations: clear() – removes all elements isEmpty() – returns true/false size() addAll(Collection<? extends E> c) removeAll(Collection<? extends E> c) retainAll(Collection<? extends E> c)

29 TreeSet – Useful methods
Additional, order-related, operations: first() – returns the first (lowest) element last () – returns the last (highest) element descendingIterator() - Returns an iterator over the elements in this set in descending order, from highest (last) to lowest (first). Even more methods…

30 Debugger Basics

31 Debugging, the Naïve Way

32 Using a Debugger So far when we wanted to test our code we’ve just executed it (Using Ctrl+F11 from eclipse, for instance). Java code can also be executed using a Debugger. Debugger: A piece of software that executes our code, and allows us to inspect the different states of our program throughout its execution.

33 Using a Debugger Eclipse already has an integrated debugger.
To execute your code using a Debugger, press F11 instead of Ctrl+F11.

34 Debugger: Breakpoints
Breakpoints allow you to stop the code execution and inspect the program’s current state:

35 Debugger: Breakpoints
Breakpoints allow you to stop the code execution and inspect the program’s current state: Ctrl+Shift+B

36 Debugger: Breakpoints
Breakpoints allow you to stop the code execution and inspect the program’s current state: The program has halted when it reached our breakpoint. But what can we do now ?

37 Debugger: Inspecting Variables
It would be very useful to inspect the values inside the different variables. What is the current value of “i” ? If we mark the variable name and press Ctrl+Shift+I we’ll get the following: Also on mouse-over:

38 Debugger: Inspecting Variables
In case we want to inspect multiple variables, we can use the “Variables” debug view:

39 Debugger: Inspecting Variables
In case we want to inspect multiple variables, we can use the “Variables” debug view:

40 Debugger: Inspecting Expressions
We can also inspect complex expressions with the “Expressions” debug view:

41 Debugger: Navigating the code
Once you have stopped at a breakpoint, you can: Press F5 to step into a function. Press F7 to step out of a function. Press F6 to move to the next line. Press F8 to continue executing the code (until the next breakpoint).

42 Debugger: Navigating the code
Once you have stopped at a breakpoint, you can: Press F5 to step into a function. Press F7 to step out of a function. Press F6 to move to the next line. Press F8 to continue executing the code (until the next breakpoint). F5 will step into “processNumber” method code.

43 Debugger: Conditional Breakpoints
What if something goes wrong only when i == 23 and j == 23? What can we do to debug our code efficiently ?

44 Debugger: Conditional Breakpoints
We can add a temporary condition to our code and add a breakpoint when it holds. Highly not recommended. Breakpoint

45 Debugger: Conditional Breakpoints
We can use a Conditional Breakpoint.


Download ppt "OOP Tirgul 5."

Similar presentations


Ads by Google