Saturday, 31 May 2014

ArrayList : remove(int index) : Removes the element at the specified position in this list.

public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left.

import java.util.*;

class ArrayListRemoveAtIndex{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("\nRemove Element at index 2");
  System.out.print("Element removed is ");
  System.out.println(myList.remove(2));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40]

Remove Element at index 2
Element removed is 30

Elements in myList are
[10, 20, 40]

1. Throws IndexOutOfBoundsException, if index is out of range
import java.util.*;

class ArrayListRemoveAtIndexOut{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.println("\nRemove Element at index 5");
  System.out.print("Element removed is ");
  System.out.println(myList.remove(5));
  
  System.out.println("\nElements in myList are");
  System.out.println(myList);
 }
}

Output
Elements in myList are
[10, 20, 30, 40]

Remove Element at index 5
Element removed is Exception in thread "main" java.lang.IndexOutOfBoundsExceptio
n: Index: 5, Size: 4
        at java.util.ArrayList.rangeCheck(ArrayList.java:638)
        at java.util.ArrayList.remove(ArrayList.java:477)
        at ArrayListRemoveAtIndexOut.main(ArrayListRemoveAtIndexOut.java:19)


Prevoius                                                 Next                                                 Home

Friday, 30 May 2014

ArrayList : listIterator : Iterate the List

ListIterator<E> listIterator()
List Provides way to iterate the List both forward and backward way. Methods equals, hashCode, listIterator, removeRange, subList are inherited from AbstractList.

import java.util.*;

class ArrayListIterator{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  myList.add(50);
  
  /* traverse the list in Forward */
  ListIterator<Integer> iter = myList.listIterator();
  System.out.println("Forward Traversal Of the List is");
  while(iter.hasNext()){
   System.out.print(iter.next() + " ");
  }
  System.out.println();
  
  /* traverse the list in Backward */
  System.out.println("Backward Traversal Of the List is");
  while(iter.hasPrevious()){
   System.out.print(iter.previous() + " ");
  }
  System.out.println();
  
 }
}


Output
Forward Traversal Of the List is
10 20 30 40 50
Backward Traversal Of the List is
50 40 30 20 10


Prevoius                                                 Next                                                 Home

ArrayList : lastIndexOf : Get the last occurrence index of Element

public int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

import java.util.*;
 
class ArrayListLastIndexOf{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(30);
  myList.add(20);
  myList.add(30);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Last Index of 30 is ");
  System.out.println(myList.lastIndexOf(30));
 }
}

Output
Elements in myList are
[30, 20, 30]
Last Index of 30 is 2


Prevoius                                                 Next                                                 Home

ArrayList : iterator : Returns Iterator over List

public Iterator<E> iterator()
Returns an iterator over the elements in this list. This implementation returns a list iterator over the list.

import java.util.*;
class ArrayListIterator1{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  Iterator<Integer> myIter;
  
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  myIter = myList.iterator();
  
  System.out.println("Elements in myList are");
  while(myIter.hasNext()){
   System.out.println(myIter.next());
  }
 }
}

Output
Elements in myList are
10
123
321
456

Prevoius                                                 Next                                                 Home

ArrayList : isEmpty : Check Whether List is empty

boolean isEmpty()
Returns true if this list contains no elements.

import java.util.*;
class ArrayListEmpty{
 public static void main(String args[]){
  ArrayList<Double> myList = new ArrayList<> ();
  
  System.out.println("Number Of Elements in the List are " + myList.size());
  System.out.println("Is the List Empty " + myList.isEmpty());
  
  /* Add Elements to the List */
  myList.add(10.01);
  myList.add(null);
  myList.add(123.01);
  myList.add(null);
  
  System.out.println("Number Of Elements in the List are " + myList.size());
  System.out.println("Is the List Empty " + myList.isEmpty());
 }
}

Output
Number Of Elements in the List are 0
Is the List Empty true
Number Of Elements in the List are 4
Is the List Empty false

Prevoius                                                 Next                                                 Home

ArrayList : indexOf : get the index of Element

public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

import java.util.*;
 
class ArrayListIndexOf{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Index of 30 is ");
  System.out.println(myList.indexOf(30));
 }
}

Output
Elements in myList are
[10, 20, 30]
Index of 30 is 2


Prevoius                                                 Next                                                 Home

ArrayList : get : Get the Element at Specific index

public E get(int index)
Returns the element at the specified position in this list.

import java.util.*;

class ArrayListGet{
  public static void main(String args[]){
    ArrayList<Integer> myList;
    myList = new ArrayList<Integer> ();
  
    /* Add Elements to myList */
    myList.add(10);
    myList.add(20);
    myList.add(30);
    myList.add(40);
  
    System.out.println("Elements in myList are " + myList);
    System.out.println("\nElement at index 1 is " + myList.get(1));
  }
}


Output
Elements in myList are [10, 20, 30, 40]

Element at index 1 is 20

1. Throws IndexOutOfBoundsException, if index is out of range
import java.util.*;

class ArrayListGetIndexOut{
  public static void main(String args[]){
    ArrayList<Integer> myList;
    myList = new ArrayList<Integer> ();
  
    /* Add Elements to myList */
    myList.add(10);
    myList.add(20);
    myList.add(30);
    myList.add(40);
  
    System.out.println("Elements in myList are " + myList);
    System.out.println("\nElement at index -1 is " + myList.get(-1));
  }
}


Output
Elements in myList are [10, 20, 30, 40]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
        at java.util.ArrayList.elementData(ArrayList.java:371)
        at java.util.ArrayList.get(ArrayList.java:384)
        at ArrayListGetIndexOut.main(ArrayListGetIndexOut.java:15)

Prevoius                                                 Next                                                 Home