View RSS Feed

Algorithm and Data Structure

Algorithm and data structure examples in Java.

  1. Java Linked List Implementations

    by , 02-19-2012 at 06: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 06: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++)
    ...
    Categories
    Linked List
  3. Accessing Items from a List

    by , 02-19-2012 at 06: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:
    System.out.println(MyList.size());
    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 06:47 AM
    Elements are added to list by using these 4 different methods given below:
    Java Code:
    MyList.add("1");
    //or
    MyList.add(varName);
    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");
    //or
    MyList.add(0, varName);
    Zero tells the element location, as given in above code. This is basically added as 1st element.
    Other two ...
    Categories
    Linked List