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. BufferedReader
BufferedReader bufferedreader = new
BufferedReader(new InputStreamReader(System.in));
int age = bufferedreader.read();
Methods
String name = bufferedreader.readLine();
int read()
String readLine()
6. StringTokenizer
•It can be used to accept multiple inputs from console in a single
line 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 OutputStream
FileInputStream FileOutputStream
Reader Writer
DataInputStream DataOutputStream
May be buffered or unbuffered FileReader FileWriter
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
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 ListIterator
ListIterator 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
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.
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.
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.