Saturday, 4 June 2016

Difference between ArrayList and Array


ArrayList is a variable length Collection class which means its size gets increased to half of its capacity once its initial size is full.

Array is fixed length data structure which means one cannot change its length after creating the array object.

ArrayList can only contains objects.It cannot contain primitive types i.e (int,float,long)

Arrays can contain both objects as well as primitive types.

Apart from for and for each loop,Iterator can also be used to iterate ArrayList.

For and for each loop can be used to iterate over Arrays.

ArrayList is single dimensional.
For ex:-
ArrayList<String> a=new ArrayList<String>();

Arrays can have multi dimensions also.
For ex:- Integer [][] a=new Integer[3][3];

In ArrayList ,elements are added using add(e) method.
For ex:- ArrayList<String> a=new ArrayList<String>();

In Arrays,elements are added using assignment operator.
For ex:-
Integer a[]=new Integer[3];
a[0]=new Integer(1);

Enjoy Reading

Difference between Queue and Stack

Queue is a collection of objects works on mechanism of First In First Out (FIFO) which means object which is added first, will be removed first.
Stack is collection of objects works on the principle of Last In First Out (LIFO) which means object which is added first will be removed last.
In Queue, Insertion and deletion operation are performed at different end i.e insertion is performed at one end (front) and deletion is performed at another end (rear).
In Stack, Insertion and deletion are performed at same end i.e at one side of the stack (top).
Queue consist of two pointers front and rear. Front points to the first element and rear points to last element
Stack consist of only one pointer i.e top which points to the last element.
Queue is said to be empty if front is equal to rear.
Stack is said to be empty if top is equal to -1.
Real Time Example:-Standing in line in Bank to deposit amount.
Real Time Example:-Distributing set of plates at the buffet.
Technical example:- sending imq messages
Technical example:-Checking for equal number of parenthesis in an expression.(a+b)
Common methods of queue are:-add(e),remove(),element(),offer(e),poll(),peek().
Common methods are :-push(e),pop(),peek(),empty(),search(e).

Enjoy Reading.

Sunday, 29 May 2016

Difference between Comparable and Comparator interfaces ?

Comparable and Comparator interface are two interfaces used to compare and sort the objects.Below are some basic differences between Comparable and Comparator interface.

It uses the compareTo() method.
int objectOne.compareTo(objectTwo).
it uses the compare() method.

int compare(ObjOne, ObjTwo)
It is necessary to modify the class whose instance is going to be sorted.
A separate class can be created in order to sort the instances.
Only one sort sequence can be created.
Many sort sequences can be created.
It is frequently used by the API classes.
It used by third-party classes to sort instances.

To know more about the Comparator,refer this example Comparator Example

Enjoy Reading