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.