Tuesday, November 6, 2012

If two classes are within the same package you dont have to use import statement

(a)
package pack1;
public class ClassA extends ClassB {

    public static void main(String[] args) {
        /*
          You can see from here when we are importing a class within the same package
          say pack1 you need not explicitly import the package say import pack1.ClassB
          also in the class A if you see we have not given any declaration for the integer
          variable i (without any declaration it will take it as default or else protected or public)
        */
        ClassB obj = new ClassB();
        System.out.println("The Output in Class A is " + obj.i);

    }

}

(b)
package pack1;
public class ClassB {
    //Note : If you declare it as private it will show error in ClassA as the classes are not same
    //Can be declared protected, public or (just like that..that is default)
    int i=10;
}

Answer
----------
The Output in Class A is 10

How to use size method in java

import java.util.*;
public class size {

    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        a1.add("1");
        a1.add("2");
        a1.add("3");
        System.out.println("\n ArrayList size is "+a1.size());
    }

}

Answer
----------
ArrayList size is 3

retainAll method of Collection interface in java

import java.util.*;
public class retainAll {
    public static void main(String[] args) {


        ArrayList a1 = new ArrayList();
        a1.add(1);
        a1.add(2);
       
        ArrayList a2 = new ArrayList();
        a2.add(3);
        a2.add(4);       
       
        a2.add(a1);
        for(int i=0;i<a2.size();i++)
        {
            System.out.println("a2 list is " + a2.get(i));
        }       
        System.out.println("");
        boolean retainCheck;
       
        retainCheck = a2.retainAll(a1);
        /*
        a2.retainAll(a1) will delete all the elements of the invoked collection (a1) and will retain 
        all the elements of the invoking collection a2
        */
       
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("a1 list is " + a1.get(i));
        }
        System.out.println("");
        System.out.println("a2's size is " + a2.size());
        for(int i=0;i<a2.size();i++)
        {
            System.out.println("a2 list is " + a2.get(i));
        }       
       
    }
}

Answer
----------
a2 list is 3
a2 list is 4
a2 list is [1, 2]

a1 list is 1
a1 list is 2

a2's size is 0

How to use iterator method in java

/*
iterator() method is defined as  - Iterator iterator in Collection interface
*/
import java.util.*;
public class iteratorMethod {
    public static void main(String[] args) {


        ArrayList a1 = new ArrayList();
        a1.add("a");
        a1.add("b");
        a1.add("c");
        a1.add("d");
       
        Iterator itr = a1.iterator();
        while(itr.hasNext())
        {
            Object instance = itr.next();
            System.out.println("The Output is " + instance);
        }
       
    }
}

Answer
----------
The Output is a
The Output is b
The Output is c
The Output is d

isEmpty method of Collection interface in java

/*
   isEmpty() method is defined as - boolean isEmpty() in Collection interface
*/
import java.util.*;
public class isEmpty {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");   
       
        boolean removeCheck;
        removeCheck = a1.remove(a1);
        removeCheck = a1.remove(a1);
        //.....
       
        System.out.println("The ArrayList on performing the a1.remove(a1) method) " + a1.isEmpty());
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The ArrayList elements (after the remove() method) are " + a1.get(i));
        }
        /*
        because a1.remove(a1) tries to remove the entire elements present in a1 but since boolean 
        remove(Object obj) is intended to remove only one instance object from the invoking  
        collection something like this...below would be the correct code
        */
        removeCheck = a1.remove(a1.get(0));
        removeCheck = a1.remove(a1.get(1));
        for(int i=0;i<a1.size();i++)
        {       
            System.out.println("\n The ArrayList will now decrease (after performing the remove(a1.get(i)) method) " + a1.get(i) + "\n");
        }
       
        a1.add("Four");
        a1.add("Five");
        a1.add("Six");       
        removeCheck = a1.removeAll(a1);
        System.out.println("The ArrayList should be empty (after removeAll() method) " + a1.isEmpty() + " as the size is now " +a1.size() + "\n");
       
        //The below does not gets displayed because the
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The ArrayList elements (after the removeAll() method) are" + a1.get(i));
        }       
               
    }

}

Answer
--------
The ArrayList on performing the a1.remove(a1) method) false
The ArrayList elements (after the remove() method) are One
The ArrayList elements (after the remove() method) are Two
The ArrayList elements (after the remove() method) are Three

The ArrayList will now decrease (after performing the remove(a1.get(i)) method) Two

The ArrayList should be empty (after removeAll() method) true as the size is now 0

indexOf method in List Interface in Java

/*
indexOf method is defined as  - int indexOf(Object obj) in List Interface
*/

import java.util.*;
public class indexOf {

    public static void main(String[] args) {
       
        ArrayList a1 = new ArrayList();
        a1.add(0,"a");
        a1.add(1,"b");
        a1.add(2,"c");
        a1.add(3,"d");
       
        System.out.println("The size of a1 is " + a1.size());
        System.out.println("The Value of a1 is " + a1.indexOf("c"));
    }

}

Answer
----------
The size of a1 is 4
The Value of a1 is 2

hashCode in Java

import java.util.ArrayList;
public class hashCode {

    public static void main(String[] args) {

        ArrayList a1 = new ArrayList();
        a1.add("One");
        a1.add("Two");
        a1.add("Three");
       
        int hashCodeforInvokingCollection = 0;
        for(int i=0;i<a1.size();i++)
        {
            //hashCodeforInvokingCollection = a1.get(i).hashCode();
            hashCodeforInvokingCollection = a1.get(i).hashCode();
            System.out.println(" a1's "+a1.get(i)+" " +hashCodeforInvokingCollection);
        }       
       
    }
}

Answer
----------
a1's One 79430
a1's Two 84524
a1's Three 80786814

How to use get Method in ArrayList in Java

/*
List interface defines the method as - Object get(int index)
*/
import java.util.ArrayList;
public class getMethod {

    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        for(int i=1;i<=3;i++)
        {
            a1.add(i);
        }
       
        System.out.println("The size of a1 is " + a1.size());
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The Value of a1 is " + a1.get(i));
        }
    }

}

Answer
----------
The size of a1 is 3
The Value of a1 is 1
The Value of a1 is 2
The Value of a1 is 3

IndexOutOfBoundsException in add Method of List Interface in Java

import java.util.*;
public class addError {
    public static void main(String[] args) {

    //Error (a)
    ArrayList a1 = new ArrayList();
    a1.add(0, "0");
    a1.add(1, "after 0 ");
    a1.add(2, "after 1 ");
    a1.add(4, "after 2 ");
    /*
       a1.add(4) ? what is this ?....
    */
       
    /*
       The above and below Array add method will show an IndexOutOfBoundsException because 
       in list (interface) the method's index should follow sequentially starting from 0 to n but in the 
       list above we have added the 4th index directly after 2nd index and hence the list adds upto 
       0,1,2 but after 2 when it is expecting the 3rd index to be entered in the memory storage... the 
       4th element is entered in memory and hence it throws IndexOutOfBoundsException as list 
       stores in memory only sequentially.
   
       Index should start from 0 and not from 1, the below add method shows error since the first 
       index should be 0 and following sequentially
    */
    //Error (b)
    a1.add(1, "1");
    a1.add(2, "after 1 ");
    a1.add(3, "after 2 ");
    a1.add(4, "after 3 ");
   
  }
}

Answer :
------------
/*
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
    at java.util.ArrayList.add(ArrayList.java:426)
    at addError.main(addError.java:10)

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
    at java.util.ArrayList.add(ArrayList.java:426)
    at addError.main(addError.java:9)
*/

addAll method of List interface in Java

/*
addAll Method is defined as boolean addAll(int index, Collection c)
*/

import java.util.*;
public class addAll {

    public static void main(String[] args) {

        ArrayList a1 = new ArrayList();
        for(int i=1;i<=3;i++)
        {
            a1.add(i);
        }
       
        System.out.println("The size of a1 is " + a1.size());
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The Value of a1 is " + a1.get(i));
        }
       
        ArrayList a2 = new ArrayList();
        a2.addAll(0, a1);
        /*
          Here if you see even after adding the collection element a1 to addAll method of ArrayList a2
          the size and value of a2 collection element remains the same as a1 
        */
        System.out.println("\nThe size of a2 is " + a2.size());
        for(int i=0;i<a2.size();i++)
        {
            System.out.println("The Value of a2 is " + a2.get(i));
        }

        ArrayList a3 = new ArrayList();
        a3.addAll(0, a2);
        /*
          Not only that even if you add collection element a2 to addAll method of ArrayList a3
          the size and value of a3 collection element remains the same as a1 or a2.
        */
        System.out.println("\nThe size of a3 is " + a3.size());
        for(int i=0;i<a3.size();i++)
        {
            System.out.println("The Value of a3 is " + a3.get(i));
        }       
       
    }

}

Answer
--------

The size of a1 is 3
The Value of a1 is 1
The Value of a1 is 2
The Value of a1 is 3

The size of a2 is 3
The Value of a2 is 1
The Value of a2 is 2
The Value of a2 is 3

The size of a3 is 3
The Value of a3 is 1
The Value of a3 is 2
The Value of a3 is 3