Thursday, August 4, 2011

Remove an element from specified index of Java ArrayList

package org.best.example;
   
    /*
      Remove an element from specified index of Java ArrayList Example
      This Java Example shows how to remove an element at specified index of java
      ArrayList object using remove method.
    */
    
    import java.util.ArrayList;
    
    public class RemoveElementFromArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    /*
      To remove an element from the specified index of ArrayList use
      Object remove(int index) method.
      It returns the element that was removed from the ArrayList.
      */
    Object obj = arrayList.remove(1);
    System.out.println(obj + " is removed from ArrayList");
    
    System.out.println("ArrayList contains...");
    //display elements of ArrayList
    for(int index=0; index < arrayList.size(); index++)
    System.out.println(arrayList.get(index));
    
    }
    }
    
    /*
    Output would be
    2 is removed from ArrayList
    ArrayList contains...
    1
    3
    */

Wednesday, August 3, 2011

Java Questions - 31

If you're overriding the method equals() of an object, which other method you might also consider?

hashCode()

Remove all elements from Java ArrayList

package org.best.example;
   
/*
      Remove all elements from Java ArrayList Example
      This Java Example shows how to remove all elements from java ArrayList object
      using clear method.
    */
    
    import java.util.ArrayList;
    
    public class RemoveAllElementsOfArrayListExample {
    
    public static void main(String[] args) {
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    
    System.out.println("Size of ArrayList before removing elements : "
    + arrayList.size());
    /*
      To remove all elements from the ArrayList use
      void clear() method.
      */
    arrayList.clear();
    System.out.println("Size of ArrayList after removing elements : "
    + arrayList.size());
    
    }
    }
    /*
    Output would be
    Size of ArrayList before removing elements : 3
    Size of ArrayList after removing elements : 0
    */

Tuesday, August 2, 2011

Java Questions - 30

You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?

Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

Iterate through elements Java ArrayList using ListIterator

    package org.best.examples;

    /*
      Iterate through elements Java ArrayList using ListIterator Example
      This Java Example shows how to iterate through the elements of java
      ArrayList object in forward and backward direction using ListIterator.
    */
    
    import java.util.ArrayList;
    import java.util.ListIterator;
    
    public class IterateThroughArrayListUsingListIteratorExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    /*
      Get a ListIterator object for ArrayList using
      listIterator() method.
      */
    ListIterator itr = arrayList.listIterator();
    
    /*
      Use hasNext() and next() methods of ListIterator to iterate through
      the elements in forward direction.
      */
    System.out.println("Iterating through ArrayList elements in forward
    direction...");
    while(itr.hasNext())
    System.out.println(itr.next());
    
    /*
      Use hasPrevious() and previous() methods of ListIterator to iterate through
      the elements in backward direction.
      */
    System.out.println("Iterating through ArrayList elements in backward
    direction...");
    while(itr.hasPrevious())
    System.out.println(itr.previous());
    
    }
    }
    
    /*
    Output would be
    Iterating through ArrayList elements...
    Iterating through ArrayList elements in forward direction...
    1
    2
    3
    4
    5
    Iterating through ArrayList elements in backward direction...
    5
    4
    3
    2
    1
    */

Monday, August 1, 2011

Iterate through elements Java ArrayList using Iterator

    package org.best.examples;

    /*
      Iterate through elements Java ArrayList using Iterator Example
      This Java Example shows how to iterate through the elements of java
      ArrayList object using Iterator.
    */
    
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class IterateThroughArrayListUsingIteratorExample {
    
    public static void main(String[] args) {
    
    //create an ArrayList object
    ArrayList arrayList = new ArrayList();
    
    //Add elements to Arraylist
    arrayList.add("1");
    arrayList.add("2");
    arrayList.add("3");
    arrayList.add("4");
    arrayList.add("5");
    
    //get an Iterator object for ArrayList using iterator() method.
    Iterator itr = arrayList.iterator();
    
    //use hasNext() and next() methods of Iterator to iterate through the elements
    System.out.println("Iterating through ArrayList elements...");
    while(itr.hasNext())
    System.out.println(itr.next());
    
    }
    }
    
    /*
    Output would be
    Iterating through ArrayList elements...
    1
    2
    3
    4
    5
    */

Java Questions - 29

What's the difference between a queue and a stack?

Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule