1602-19-733-014-Week8-Oop Lab
1602-19-733-014-Week8-Oop Lab
3) This difference between Serializable and Externalizable is performance. You can not
do much to improve performance of default serialization process except reducing
number of fields to be serialized by using transient and static keyword but with
Externalizable interface you have full control over Serialization process.
Extends: Extends is a keyword that is used for developing inheritance between two
classes and two interfaces.
Implements: Implements is a keyword used for developing inheritance between class
and interface.
Elements can be inserted or accessed by their position in the list, using a zero-
based index.
CollectionsDemo {
a1.add("Zara");
a1.add("Mahnaz");
a1.add("Ayan");
System.out.print("\t" + a1);
l1.add("Zara");
l1.add("Mahnaz");
l1.add("Ayan");
System.out.println();
System.out.print("\t" + l1);
Output
ArrayList Elements
LinkedList Elements
The Set interface contains only methods inherited from Collection and adds
the restriction that duplicate elements are prohibited.
Set also adds a stronger contract on the behavior of the equals and hashCode
operations, allowing Set instances to be compared meaningfully even if their
implementation types differ.
5; i++) {
set.add(count[i]);
System.out.println(set);
System.out.println(sortedSet);
} catch(Exception e)
{}
We can use ListIterator to traverse list only, we cannot traverse set using ListIterator.
Using ListIterator, we can traverse a List in both the directions (forward and
Backward).
3) We cannot obtain indexes while using Iterator
We can obtain indexes at any point of time while traversing a list using ListIterator.
The methods nextIndex() and previousIndex() are used for this purpose.
We can add element at any point of time while traversing a list using ListIterator.
By using set(E e) method of ListIterator we can replace the last element returned by
next() or previous() methods.
6) Methods of Iterator:
hasNext()
next()
remove()
Methods of ListIterator:
add(E e)
hasNext()
hasPrevious()
next()
nextIndex()
previous()
previousIndex()
remove()
set(E e)
this interface has precise control over where in the list each element is
inserted. The user can access elements by their integer index (position in
Fields that are marked as transient can not be part of the serialization
and deserialization. We don't want to save the value of any variable then
variable.
Student implements
java.io.Serializable
public String
studentname;
Student(int a, String b)
{ this.rollno= rollno;
this.studentname= studentname;
} class
Test
{ public static void main(String[] args)
// Serialization
try
{
//Saving of object in a file
out.writeObject(studobject);
out.close();
file.close();
System.out.println("Object has been serialized");
catch(IOException ex)
System.out.println("IOException is caught");
try
studobject1 = (Student)in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized ");
catch(IOException ex)
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
System.out.println("ClassNotFoundException is caught");
}
}
OUTPUT;
Object has been serialized
Object has been deserialized
Roll no =1
Studentname=chinmin
8.2. Write a program to demonstrate Arraylist and demonstrate all the methods A)
import java.util.*; class JavaExample{ public static void main(String args[]){
ArrayList<String> alist=new ArrayList<String>();
alist.add("Adam");
alist.add("Tom");
alist.add("Lucky");
alist.add("Peora");
alist.add("Ankush");
alist.add("Tiger");
//displaying elements
System.out.println(alist);
//displaying elements
System.out.println(alist);
alist.remove("Adam");
alist.remove("Ankush");
//displaying elements
System.out.println(alist);
//displaying elements
System.out.println(alist);
alist.set(0, "tittat");
System.out.println(alist);
}
}
OUTPUT:
[Adam,Tom,Lucky,Peora,Ankush,Tiger]
[Adam,Tom,Lucky,Adam,Peora,Ankush,Tiger]
[Tom,Lucky,Peora,Tiger]
[tittat,Lucky,Peora,Tiger]
8.3. Write a program to demonstrate Linkedlist and demonstrate all the methods
A) import
java.util.*;
public class LinkedListExample { public
static void main(String args[]) {