Friday, December 29, 2017

Stack in java


Its a child class of Vector. it is designed the class for LIFO last in first out order.

Constructors :
  1. Stack s=new Stack();

                                                     
 
offset Stack index
1 D 3
2 C 2
3 B 1
4 A 0

methods:

 1. object push(object o)
      to insert an object into the stack.
 2. object pop()
     to remove an object and returns top of the stack object.
3.  object peek()
    to return top of the stack without removal.
4. Boolean empty()
   return true if the stack is empty.
5. int search(object o)
   returns offset if the element is available otherwise -1.

note : stack is extends vector so all methods of vector class are available for the stack too.


example :


package indrajeet.list.exp;

import java.util.Stack;

public class StackExp {

public static void main(String[] args) {

Stack s=new Stack<>();
s.push(3);
s.push("indajeet");
s.add(3);

System.out.println(s);
System.out.println(s.peek());// return top of the element without removal
System.out.println(s);
System.out.println(s.pop());//return top of the element with removal
System.out.println(s);
System.out.println(s.search(2));// returns offset if element is available otherwise -1


}

}

output :

[3, indajeet, 3]
3
[3, indajeet, 3]
3
[3, indajeet]
-1


Thursday, December 28, 2017

Vector in Java


Vector also implements List interface. So its also having same feature as ArrayList having like
underline data structure is re sizable array.  below are the same features which are followed.

1. insertion order is preserved.
2. duplicates are allowed.
3. heterogeneous objects are allowed.
4. null insertion is possible
5. Its implements serializable , clonable  and RandomAccess interfaces.

but one point to need to remember which is different from others every methods which are in the vector are synchronized So we can say vector objects are thread safe.

Vector Constructors :

 1. Vector v=new Vector();

as above creates empty vector object which is having default initial capacity 10.

once vector reaches its max capacity then a new vector object will be created with new capacity.

  new capacity= current capacity*2.

as above new capacity will be twice of current capacity.
if current capacity is 10 then new object capacity will be 20.

2. Vector v1=new Vector(int initial capacity);

creates an empty vector objects with specified initial capacity.
Vector v1= new Vector(100);

3. Vector v3=new Vector(int initial capacity, int increment capacity );
 Vector v3 new Vector(100, 5);

as above define vector object v3 which is having initial capacity 100  which will be incremented by 5.

4. Vector v4-new Vector(Collection c);
creates an equivalent object of Vector for the given collection object. this constructor means inter conversion.


Vector Specific Methods:

to add methods:
 1. addElement(object o) // Vector method
 2. add(object o) // Collection method
 3. add(int index, object o) // List

to remove methods:
 1.  remove(object o) // Collection method
 2. removeElement(object o) // Vector method
 3. remove(object o)    // List
 4. clear() // Collection
 5. removeAllElement() // Vector
to get methods:

 1. object get (int index) // LIst
 2. object elementAt(int index) //  Vector
 3. objet firstElement() // vector
 4. objet lastElement() // vector

other methods:

 1. int size();
2. int capacity();
3. Enumeration elements();



as below example we can use above methods and constructors.

package indrajeet.list.exp;

import java.util.Enumeration;
import java.util.Vector;

public class VectorExp {

public static void main(String[] args) {


Vector v=new Vector<>();
System.out.println(v.capacity()); //10
v.add("indraeet");
v.addElement("singh");
v.add(1, 4);
System.out.println(v); //[indraeet, 4, singh]
v.removeElementAt(1);

System.out.println(v);  //[indraeet, singh]
System.out.println(v.size()); //2

Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println( e.nextElement());
}

}

}

Wednesday, December 27, 2017

LinkedList


LinkedList implements List interface. The underline data structure  is doubly linkedlist.

there are some point which are followed by linked list.

1. Insertion order is preserved.
2. duplicates are allowed.
3. heterogeneous is allowed.
4. Null insertion is possible.
5. LinkedList implements serializable and clonable interface but not randomAccess.
6. LinkedList is best choice if our frequent operation is insertion or deletion in the middle.
7. LinkedList the worst choice if our frequent operation is retrieval.


Constructors:

LinkedList l=new LinkedList();

creates empty linked list object.

LinkedList l1= new LinkedList(Collection c);


LinkedList Class specific  methods:

Usually we can use LinkedList to develop stack and queue to provide support for these requirement below are the methods.

1. void addFirst(Object o)
2. void AddLast(object o)
3. object getFirst()
4. object getLast()
5. object removeFirst()
6. object removeLast()


Example :

package indrajeet.list.exp;

import java.util.LinkedList;

public class LinkedListDemo {

public static void main(String[] args) {

LinkedList l=new LinkedList();
l.add("indar");
l.add(30);  //heterogeneous OBJECT
l.add(null);
l.add("indar"); // duplicate object
l.set(0,"jeet");
System.out.println(l); // ouput : [jeet, 30, null, indar]
l.removeFirst();
System.out.println(l); // output : [30, null, indar]


}

}


Tuesday, December 26, 2017

ArrayList



Arraylist implemented List interface. its having all features which is in List.

Its having the underline data structure  is re sizable array.
Duplicates are allowed.
Insertion order is preserved.
heterogeneous objects allowed.
Null insertion is possible.

constructors:

1.  arraylist l=new Arraylist();

above array list is empty and default initial capacity is 10.
Once arraylist reaches max capacity then new arraylist object will be creates with new capacity

like new capacity=(current capacity*3/2)+1

2. arraylist l1=new arrayList(int initial capacity)

 creates an empty arraylist object with specifies initial capacity.

3. Arraylist l3=new Arraylist(Collection c);

Creates am equivalent Arraylst object for given Collection.


Example:

package indrajeet.list.exp;
import java.util.*;

class ArrayListdemo{
public static void main(String args[]){

ArrayList l=new ArrayList();

   l.add("A");
   l.add(10);
   l.add(null);
   l.add("A");
 System.out.println(l);

}
}

output:  [A, 10, null, A]



ArrayList and Vector classes implements RandomAccess  interface so that ArrayList can access random elements. We can access with the same speed..

ArrayList is the best choice if our frequent operation is retrial operation(because its implements RandomAccess interface).
ArrayList is the worst choice if our frequent operation is insertion or deletion in the middle( because more shifting required).


By using array[index], you can access to any element while in a linked list you must navigate through all the list starting from fist until you get the element which you need.

access time for arrayList O(1)
   LinkedList O(n)

Wednesday, December 20, 2017

List



List is child interface of collection. If we want to represent a group of individual objects as a single entity where duplicates are not allowed in the insertion order , insertion order must be preserved then we should go for List.


We can preserve insertion order by the index and we can differentiate to duplicate objects by using index.

So index will play very important role in a List.

Below is the example:

B C D E A
0 1 2 3 4 5    INDEX

List interface defines the following specific methods.

1. void add(int index,object o)
2. Boolean addAll(int index, Collection C)
3. object get(int index) 
4. object set(int index, object value)
5. void remove(int index, object o)





List is implemented by the below classes.

1. ArrayList
2. LinkedList
3. Vector 
 4. Stack




Tuesday, December 19, 2017

Java Access Modifier

                                                            Java Access Modifier

basically Java access Modifiers are four type as below :

1. Private
2.Default
3.Public
4. Protected


non-Access modifiers are : Static , Abstract , native and Synchronized etc.



1. Private Access Modifier : private access modifier is accessible only within class.
2. Default : it is accessible only within package.
3. Public : public access modifier is access in every where .
4. Protected : its accessible within package or outside package but throw inheritance only.



below are the table for access modifier:


Access Modifier within class within package outside package by subclass only outside package
private Yes No No No
Default Yes yes No No
Protected yes yes Yes No
public yes yes yes Yes



Example for all:



package indrajeet.com.defaultpackage.test;

public class DefaultTestClass {

int a=40;


}



package indrajeet.com.defaultpackage.test;

public class ProtectedTestClass {

protected int b=20;


protected void getAccess()
{
System.out.println(b);
}

}


package indrajeet.com.defaultpackage.test.importpackage;

import indrajeet.com.defaultpackage.test.DefaultTestClass;

public class AcessModifierMainclass {

public static void main(String[] args) {

DefaultTestClass object1=new DefaultTestClass();
//System.out.println(object1.a);  default access modifier can be access only within package.
ProtectedAccessClass object2=new ProtectedAccessClass(5);
ProtectedAccessClass object3=new ProtectedAccessClass();
 
}


}

class ProtectedAccessClass extends ProtectedTestClass {

 public ProtectedAccessClass()
 {
this(3);
 }

public ProtectedAccessClass(int i) {
System.out.println(i);
}




}


Thursday, December 14, 2017

Best explanation for this Keyword

Please go to the link https://www.javatpoint.com/this-keyword for this keyword.
best explanation is here.