Wednesday, February 25, 2015

ListIterator Example

This list iterator is used to display the elements in both the directions.


package com.listsample;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorExample {
   public static void main(String a[]){
       List<Integer> li = new ArrayList<Integer>();
       ListIterator<Integer> litr = null;
       li.add(23);
       li.add(98);
       li.add(29);
       li.add(71);
       li.add(5);
       litr=li.listIterator();
       System.out.println("Elements in forward directiton");
       while(litr.hasNext()){
           System.out.println(litr.next());
       }
       System.out.println("Elements in backward directiton");
       while(litr.hasPrevious()){
           System.out.println(litr.previous());
       }
   }
}

ArrayList Example

package com.listsample;

import java.util.*;

public class ListExample {

public static void main(String args[]) {

// Creating an empty array list

List list = new ArrayList();

// Adding items to arrayList - adding both integer and string values

list.add(2);

list.add(3);

list.add(6);

list.add("DDD");

// Display the contents of the array list

System.out.println("The arraylist contains the following elements: "

+ list);
/*
for (int i = 0; i < list.size(); i++) {

                       //This will throwClassCastingException

Integer iVal = Integer.valueOf(list.get(i).toString());
System.out.println("Index: " + i + " - Item: " + iVal);

}
// hasNext(): returns true if there are more elements
// next(): returns the next element

System.out.println("Retrieving items using iterator");
Iterator itr = list.iterator();

while (itr.hasNext()) {
String str = itr.next().toString();
System.out.println(str);
if (str.equals("AAA"))
itr.remove();
}
*/
// Generics - to restrict the collection object to specific data type

ArrayList<String> list1 = new ArrayList<String>();

list1.add("aaaa");
list1.add("2");

System.out.println("Availabiliy of the element " +list1.contains("bbb"));


ArrayList<String> list2 = new ArrayList<String>();
System.out.println("Before adding theelemets : "+list2.isEmpty());
list2.add("bbbb");
list2.add("aaaa");
list2.add("cccc");
list2.add("2");
list2.add("cccc");

                  //tostring()is to display the elements as string with comma separated values
System.out.println("String list "+list2.toString());

System.out.println("Position of the first occurence of the element "+list2.indexOf("cccc")+" the position of the duplicate "+list2.lastIndexOf("cccc"));

System.out.println("After adding the elements: "+list2.isEmpty());

System.out.println("Comparing the lists: "+list1.containsAll(list2));

Iterator<String> itr1 = list1.iterator();

while (itr1.hasNext()) {
String s = itr1.next();
System.out.println(s);
}

}


}

Friday, February 20, 2015

Array List Example


import java.util.*;

 

public class ArrayListExamples {

 

    public static void main(String args[]) {

        // Creating an empty array list

        ArrayList<String> list = new ArrayList<String>();

 

        // Adding items to arrayList

        list.add("Item1");

       list.add("Item2");

        list.add(2, "Item3"); // it will add Item3 to the third position of

                                // array list

        list.add("Item4");

 

        // Display the contents of the array list

        System.out.println("The arraylist contains the following elements: "

                + list);


        // Checking index of an item

       int pos = list.indexOf("Item2");

        System.out.println("The index of Item2 is: " + pos);

 

       // Checking if array list is empty

        boolean check = list.isEmpty();

       System.out.println("Checking if the arraylist is empty: " + check);

 

        // Getting the size of the list

        int size = list.size();

        System.out.println("The size of the list is: " + size);

 

       // Checking if an element is included to the list

        boolean element = list.contains("Item5");

        System.out

                .println("Checking if the arraylist contains the object Item5: "

                        + element);

 

        // Getting the element in a specific position

        String item = list.get(0);

        System.out.println("The item is the index 0 is: " + item);

 

        // Retrieve elements from the arraylist

 

        // 1st way: loop using index and size list

        System.out

                .println("Retrieving items with loop using index and size list");

        for (int i = 0; i < list.size(); i++) {

            System.out.println("Index: " + i + " - Item: " + list.get(i));

        }

 

        // 2nd way:using foreach loop

        System.out.println("Retrieving items using foreach loop");

        for (String str : list) {

            System.out.println("Item is: " + str);

        }

 

        // 3rd way:using iterator

        // hasNext(): returns true if there are more elements

        // next(): returns the next element

        System.out.println("Retrieving items using iterator");

        for (Iterator<String> it = list.iterator(); it.hasNext();) {

            System.out.println("Item is: " + it.next());

        }

 

        // Replacing an element

        list.set(1, "NewItem");

        System.out.println("The arraylist after the replacement is: " + list);

 

        // Removing items

        // removing the item in index 0

        list.remove(0);

 

        // removing the first occurrence of item "Item3"

        list.remove("Item3");

 

        System.out.println("The final contents of the arraylist are: " + list);

 

        // Converting ArrayList to Array

        String[] simpleArray = list.toArray(new String[list.size()]);

        System.out.println("The array created after the conversion of our arraylist is: "

                        + Arrays.toString(simpleArray));
    }

}