Your SlideShare is downloading. ×
0
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Java class 5
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Java class 5

453

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
453
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
68
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Demo
  • Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
  • Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
  • Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
  • Transcript

    • 1. Input And Output
    • 2. Input and Output• Input is the data what we give to the program.• Output is the data what we receive from the program in the form of result.• Stream represents flow of data i.e. sequence of data.• To give input we use InputStream and to receive output we use OutputStream.
    • 3. How input is read from Keyboard? connected to send data to System.in InputStream BufferedReader Reader It represents It reads data from It reads data from keyboard. To read keyboard and InputStreamReader and data from keyboard send that data to stores data in buffer. It has it should be got methods so that data connected to BufferedReader. can be easily accessed. InputStreamReader
    • 4. Reading Input from console• Input can be given either from file or keyword.• Input can be read from console in 3 ways. BufferedReader StringTokenizer Scanner
    • 5. BufferedReaderBufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));int age = bufferedreader.read(); MethodsString name = bufferedreader.readLine(); int read() String readLine()
    • 6. StringTokenizer•It can be used to accept multiple inputs from console in a singleline where as BufferedReader accepts only one input from a line.•It uses delimiter(space, comma) to make the input into tokens.BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); String input = bufferedreader.readLine();StringTokenizer tokenizer = new StringTokenizer(input, ”,”); String name = tokenizer.nextToken(); delimiter int age=tokenizer.nextToken();
    • 7. Scanner• It accepts multiple inputs from file or keyboard and divides into tokens.• It has methods to different types of input( int, float, string, long, double, byte) where tokenizer does not have. Scanner scanner = new Scanner(System.in); int rollno = scanner.nextInt();` String name = scanner.next();
    • 8. Writing output to console• The output can be written to console in 2 ways: print(String)- System.out.print(“hello”); write(int)- int input=‘i’; System.out.write(input); System.out.write(‘/n’);
    • 9. I/O Streams I/O Streams Unicode Character Oriented Byte Oriented Streams Streams InputStream OutputStream Reader Writer InputStream OutputStreamFileInputStream FileOutputStream Reader WriterDataInputStream DataOutputStream May be buffered or unbuffered FileReader FileWriter
    • 10. Array List
    • 11. ArrayList class• The ArrayList class is a concrete implementation of the List interface.• Allows duplicate elements.• A list can grow or shrink dynamically• On the other hand array is fixed once it is created. – If your application does not require insertion or deletion of elements, the most efficient data structure is the array
    • 12. ArrayList class Java.util.ArrayList size: 5 elementData 0 1 2 3 4 … … Ravi Rajiv Megha Sunny Atif
    • 13. Methods in ArrayList• boolean add(Object e) • Iterator iterator()• void add(int index, Object • ListIterator listIterator() element)• boolean addAll(Collection c) • int indexOf()• Object get(int index) • int lastIndexOf()• Object set(int index,Object element) • int index(Object element) • int size()• Object remove(int index) • void clear() Java Programming: OOP 13
    • 14. ArrayList - Insertion // Create an arraylist ArrayList arraylist = new ArrayList(); // Adding elements arraylist.add("Rose"); arraylist.add("Lilly"); arraylist.add("Jasmine"); arraylist.add("Rose"); //removes element at index 2 arraylist.remove(2);
    • 15. How to trace the elements of ArrayList? • For-each loop • Iterator • ListIterator • Enumeration Java Programming: OOP 15
    • 16. For-each loop • It’s action similar to for loop. It traces through all the elements of array or arraylist. • No need to mention size of Arraylist. • for ( String s : arraylist_name) Keyword type of data name of arraylist stored in arraylist Java Programming: OOP 16
    • 17. Iterator • Iterator is an interface Iterator Methods that is used to traverse through the elements of collection. • boolean hasNext() • It traverses only in • element next() forward direction with • void remove () the help of methods. Java Programming: OOP 17
    • 18. Displaying Items using Iterator Iterator iterator = arraylist.iterator(); while (iterator.hasNext()) { Object object = iterator.next(); System.out.print(object + " "); } Java Programming: OOP
    • 19. ListIterator • ListIterator is an ListIterator Methods interface that traverses • boolean hasNext() through the elements • element next() of the collection. • void remove () • It traverses in both forward and reverse • boolean direction. hasPrevious() • element previous() Java Programming: OOP 19
    • 20. Displaying Items using ListIterator// To modify objects we use ListIteratorListIterator listiterator =arraylist.listIterator(); while (listiterator.hasNext()) { Object object = listiterator.next(); listiterator.set("(" + object + ")"); } Java Programming: OOP
    • 21. Enumeration • Enumeration is an interface whose action Enumeration Methods is similar to iterator. • boolean • But the difference is hasMoreElement() that it have no method • element for deleting an element nextElement() of arraylist. Java Programming: OOP 21
    • 22. Displaying Items using Enumeration Enumeration enumeration = Collections.enumeration(arraylist); while (enumeration.hasMoreElements()) { Object object = enumeration.nextElement(); System.out.print(object + " "); } Java Programming: OOP
    • 23. HashMaps
    • 24. HashMap Class• The HashMap is a class which is used to perform operations such as inserting, deleting, and locating elements in a Map .• The Map is an interface maps keys to the elements.• Maps are unsorted and unordered.• Map allows one null key and multiple null values• HashMap < K, V > key value associated with key• key act as indexes and can be any objects.
    • 25. Methods in HashMap• Object put(Object key, Object value)• Enumeration keys()• Enumeration elements()• Object get(Object keys)• boolean containsKey(Object key)• boolean containsValue(Object key)• Object remove(Object key)• int size()• String toString() Java Programming: OOP 25
    • 26. HashMap Class Key Value 0 Ravi 1 Rajiv 2 Megha 3 SunnyHashMap 4 ….. . ……….. .. ……….……. … 100 Atif
    • 27. HashMap - Insertion// Create a hash mapHashMap hashmap = new HashMap();// Putting elementshashmap.put("Ankita", 9634.58);hashmap.put("Vishal", 1283.48);hashmap.put("Gurinder", 1478.10);hashmap.put("Krishna", 199.11);
    • 28. HashMap - Display// Get an iteratorIterator iterator = hashmap.entrySet().iterator();// Display elementswhile (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); System.out.print(entry.getKey() + ": "); System.out.println(entry.getValue());}
    • 29. Hashtable
    • 30. Hashtable Class• Hashtable is a class which is used to perform operations such as inserting, deleting, and locating elements similar to HashMap .• Similar to HashMap it also have key and value.• It does not allow null keys and null values.• The only difference between them is Hashtable is synchronized where as HashMap is not by default.
    • 31. Methods in Hashtable• Object put(Object key, Object value)• Enumeration keys()• Enumeration elements()• Object get(Object keys)• boolean containsKey(Object key)• boolean containsValue(Object key)• Object remove(Object key)• int size()• String toString() Java Programming: OOP 31
    • 32. Hashtable - Insertion// Create a hash mapHashtable hashtable = new Hashtable();// Putting elementshashtable.put("Ankita", 9634.58);hashtable.put("Vishal", 1283.48);hashtable.put("Gurinder", 1478.10);hashtable.put("Krishna", 199.11);
    • 33. Hashtable - Display // Using Enumeration Enumeration enumeration = hashtable.keys(); // Display elements while (enumeration.hasMoreElements()) { String key = enumeration.nextElement().toString(); String value = hashtable.get(key).toString(); System.out.println(key + ":"+value); }
    • 34. •Q& A..?
    • 35. Thanks..!

    ×