Ved -Swami Dayanand -ghazi Mehmud Dharmpal (Ex-Arya pandit) Hindi Book
Recitation 1 CS0445 Data Structures Mehmud Abliz.
-
Upload
jeffry-warren -
Category
Documents
-
view
213 -
download
0
Transcript of Recitation 1 CS0445 Data Structures Mehmud Abliz.
![Page 1: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/1.jpg)
Recitation 1 CS0445 Data Structures
Mehmud Abliz
![Page 2: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/2.jpg)
Outline
• Discuss the following features of JAVA
– Scanner (reading lines of input)
– String Manipulation
– Linked List
– Hash Map
![Page 3: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/3.jpg)
Scanner class
• A simple text scanner which can parse primitive types and strings.
• A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
![Page 4: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/4.jpg)
Example(1)
this code allows a user to read a number from System.in.
Scanner sc = new Scanner(System.in); int i = sc.nextInt();
![Page 5: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/5.jpg)
Example(2) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;
public class TextScanner {
private static void readFile(String fileName) { try { File file = new File(fileName); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
![Page 6: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/6.jpg)
Example(2) Cont. public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: java TextScanner1" + "file location"); System.exit(0); } readFile(args[0]); } }
![Page 7: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/7.jpg)
String manipulation
• Some examples of string usage:
System.out.println("abc"); String cde = "cde";System.out.println("abc" + cde);String c = "abc".substring(2,3);String d = cde.substring(1, 2);
![Page 8: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/8.jpg)
String manipulation functions
• length(): returns the length of this string.
• charAt() : returns the character at the specified index. An index ranges from 0 to length() - 1.
• indexOf(): returns the index of the first occurrence of the specified character within this string.
• substring(): returns a new string that is a substring of this string.
![Page 9: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/9.jpg)
Linked List
• LinkedList class:– can be used as a stack, queue, or double-end
ed queue.
![Page 10: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/10.jpg)
Linked List – some methods
• add(int, Object): inserts the specified element at the specified position in this list.
• add(Object): appends the specified element to the end of this list.
• contains(Object): returns true if this list contains the specified element.
• get(int): returns the element at the specified position in this list.
![Page 11: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/11.jpg)
Linked List – some methods
• size(): returns the number of elements in this list.
• isEmpty(): returns true if this list contains no elements.
![Page 12: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/12.jpg)
Example(3)class LinkedListExample { public static void main(String[] args) throws Exception { LinkedList list = new LinkedList(); list.add("Hello"); list.add("world"); list.add(0,"there"); list.add(0,"there1"); list.add(1,"here"); ListIterator itt = list.listIterator(); while (itt.hasNext()) { String line = (String) itt.next(); System.out.println(line); } } // end main}
![Page 13: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/13.jpg)
HashMap
• HashMap is hash table based implementation of the Map interface.
• The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
• This class makes no guarantees as to the order of the map.
![Page 14: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/14.jpg)
HashMap – Some methods
• put(Object, Object): associates the specified value with the specified key in this map.
• get(Object key): returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key.
![Page 15: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/15.jpg)
HashMap – Some methods
• containsKey(Object key): returns true if this map contains a mapping for the specified key.
• containsValue(Object value): returns true if this map maps one or more keys to the specified value.
![Page 16: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/16.jpg)
HashMap – Some methods
• keySet(): returns a set view of the keys contained in this map. Returned object is a Set object, and can be usedconverted to an array with toArray().
• values(): returns a collection view of the values contained in this map.
![Page 17: Recitation 1 CS0445 Data Structures Mehmud Abliz.](https://reader036.fdocuments.us/reader036/viewer/2022082818/56649e7a5503460f94b7a4f6/html5/thumbnails/17.jpg)
HashMap Example
// Create a new HashMap HashMap hash = new HashMap(); // Add values to the created HashMap hash.put("one", new Integer(1)); hash.put("two", new Integer(2)); hash.put("three", new Integer(3));