Java class 5

Preview:

DESCRIPTION

 

Citation preview

Input And Output

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.

How input is read from Keyboard?

System.in InputStreamReader BufferedReader

It represents keyboard. To read data from keyboard it should be connected to InputStreamReader

It reads data from keyboard and send that data to BufferedReader.

It reads data from InputStreamReader and stores data in buffer. It has got methods so that data can be easily accessed.

connected to send data to

Reading Input from console

• Input can be given either from file or keyword.

• Input can be read from console in 3 ways.BufferedReaderStringTokenizerScanner

BufferedReader

BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));

int age = bufferedreader.read();String name = bufferedreader.readLine(); Methods

int read()String readLine()

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(newInputStreamReader(System.in));

String input = bufferedreader.readLine();

StringTokenizer tokenizer = new StringTokenizer(input, ”,”);String name = tokenizer.nextToken();int age=tokenizer.nextToken(); delimiter

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();

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’);

I/O StreamsI/O Streams

Byte Oriented Streams Unicode Character Oriented Streams

InputStream OutputStream Reader Writer

FileInputStreamDataInputStream

FileOutputStreamDataOutputStream

InputStreamReader

OutputStreamWriter

FileWriterFileReaderMay be buffered or unbuffered

Array List

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

ArrayList class

Java.util.ArrayList size: 5

0 1 2 3 4 … …

Ravi Rajiv Megha Sunny Atif

elementData

Java Programming: OOP 13

Methods in ArrayList

• boolean add(Object e)• void add(int index, Object

element)• boolean addAll(Collection c)

• Object get(int index)• Object set(int index,Object

element)

• Object remove(int index)

• Iterator iterator()• ListIterator listIterator()

• int indexOf()• int lastIndexOf()

• int index(Object element)• int size()• void clear()

ArrayList - Insertion

// Create an arraylistArrayList arraylist = new ArrayList();

// Adding elementsarraylist.add("Rose");arraylist.add("Lilly");arraylist.add("Jasmine");arraylist.add("Rose");

//removes element at index 2arraylist.remove(2);

Java Programming: OOP 15

How to trace the elements of ArrayList?

• For-each loop• Iterator• ListIterator• Enumeration

Java Programming: OOP 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 17

Iterator

• Iterator is an interface that is used to traverse through the elements of collection.

• It traverses only in forward direction with the help of methods.

• boolean hasNext()• element next()• void remove ()

Iterator Methods

Displaying Items using Iterator

Iterator iterator = arraylist.iterator();

while (iterator.hasNext()) {Object object = iterator.next();System.out.print(object + " ");

}

Java Programming: OOP

Java Programming: OOP 19

ListIterator

• ListIterator is an interface that traverses through the elements of the collection.

• It traverses in both forward and reverse direction.

• boolean hasNext()• element next()• void remove ()• boolean

hasPrevious()• element previous()

ListIterator Methods

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

Java Programming: OOP 21

Enumeration

• Enumeration is an interface whose action is similar to iterator.

• But the difference is that it have no method for deleting an element of arraylist.

• boolean hasMoreElement()

• element nextElement()

Enumeration Methods

Displaying Items using Enumeration

Enumeration enumeration = Collections.enumeration(arraylist);

while (enumeration.hasMoreElements()) {Object object =

enumeration.nextElement();System.out.print(object + " ");

}Java Programming: OOP

HashMaps

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.

Java Programming: OOP 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()

HashMap

01234...…

100

Ravi

Megha

Atif

Rajiv

Sunny

…..

………..

……….…….

Key Value

HashMap Class

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);

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());

}

Hashtable

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.

Java Programming: OOP 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()

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);

Hashtable - Display

// Using EnumerationEnumeration enumeration = hashtable.keys();

// Display elementswhile (enumeration.hasMoreElements()) {

String key = enumeration.nextElement().toString();

String value = hashtable.get(key).toString();

System.out.println(key + ":"+value);}

•Q& A..?

Thanks..!

Recommended