View RSS Feed

Algorithm and Data Structure

Algorithm and data structure examples in Java.

  1. Java Linked List Implementations

    by , 02-19-2012 at 07:57 AM
    Collection interface is subtype of a List. Collection interface methods are also present in list interface.
    List can’t be instantiated as it is an interface. For creation of instance of the List, create concrete implementation instance of the interface. In Java Collection APIs, these options are present:

    • java.util.ArrayList
    • java.util.LinkedList
    • java.util.Vector
    • java.util.Stack

    In java.util.concurrent package, List implementations ...
  2. What is the advantage of using an Iterator compared to the get(index) method?

    by , 02-19-2012 at 07:55 AM
    One of the following methods should be used to iterate the List.

    1. Iterator
    2. get(index)

    get(index) method is quite slower in comparison with Iterator.

    Consider this example:

    Java Code: This is the code to explain Iterator
    public class TestClass {
      public static void main(String[] args) {
        int  len = 100000;
        LinkedList linkedLst = new LinkedList(); 
        ArrayList  arrayLst = new ArrayList();
        for (int m =0; m!= len; m++)
    Linked List
  3. Accessing Items from a List

    by , 02-19-2012 at 07:50 AM
    Get List Size:

    To get the List size, method “size()”is used. Size of MyList will be printed by this code.
    Java Code:
    Get Item:

    For getting a specific spot value, use get() method. In this method index of the List is provided. In case when index is not present, IndexOutOfBoundsException gets thrown.

    Element is not removed from the list by this method. Just this object is returned. This given code will ...
  4. Adding Elements to a List

    by , 02-19-2012 at 07:47 AM
    Elements are added to list by using these 4 different methods given below:
    Java Code:
    An item is added by it to end of list.
    At a particular location, elements could be added as given below.
    Java Code:
    MyList.add(0, "2");
    MyList.add(0, varName);
    Zero tells the element location, as given in above code. This is basically added as 1st element.
    Other two ...
    Linked List