Saturday 10 May 2014

Vector : subList : get the Sub List

public synchronized List<E> subList(int fromIndex, int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive.

import java.util.*;

class VectorSubList{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nGetting the sublist from 1 to 2");
  List<Integer> mySubList = myVector.subList(1,3);
  
  System.out.println("\nElements in mySubList are");
  System.out.println(mySubList);
 }
}

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

Getting the sublist from 1 to 2

Elements in mySubList are
[20, 30]

1. If fromIndex and toIndex are equal, the returned List is empty
import java.util.*;

class VectorSubListRangeEqual{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nGetting the sublist from 2 to 2");
  List<Integer> mySubList = myVector.subList(2,2);
  
  System.out.println("\nElements in mySubList are");
  System.out.println(mySubList);
 }
}

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

Getting the sublist from 2 to 2

Elements in mySubList are
[]

2. The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa.

import java.util.*;

class VectorSubListBacked{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nGetting the sublist from 1 to 2");
  List<Integer> mySubList = myVector.subList(1,3);
  
  System.out.println("\nElements in mySubList are");
  System.out.println(mySubList);
  
  System.out.println("\nAdding 1,2,3 to mySubList");
  
  mySubList.add(1);
  mySubList.add(2);
  mySubList.add(3);
  
  System.out.println("\nElements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nElements in mySubList are");
  System.out.println(mySubList);
 }
}

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

Getting the sublist from 1 to 2

Elements in mySubList are
[20, 30]

Adding 1,2,3 to mySubList

Elements in myVector are
[10, 20, 30, 1, 2, 3, 40]

Elements in mySubList are
[20, 30, 1, 2, 3]

3. throws IndexOutOfBoundsException if an endpoint index value is out of range fromIndex < 0 || toIndex > size.
import java.util.*;

class VectorSubListIndexOut{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nGetting the sublist from -1 to 2");
  List<Integer> mySubList = myVector.subList(-1,3);
 }
}

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

Getting the sublist from -1 to 2
Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex = -1
        at java.util.SubList.<init>(AbstractList.java:620)
        at java.util.RandomAccessSubList.<init>(AbstractList.java:775)
        at java.util.AbstractList.subList(AbstractList.java:484)
        at java.util.Vector.subList(Vector.java:1034)
        at VectorSubListIndexOut.main(VectorSubListIndexOut.java:18)

4. Throws IllegalArgumentException if the endpoint indices are out of order fromIndex > toIndex
import java.util.*;

class VectorSubListIllegalArg{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("\nGetting the sublist from 2 to -1");
  List<Integer> mySubList = myVector.subList(2,-1);
 }
}

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

Getting the sublist from 2 to -1
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(2) > to
Index(-1)
        at java.util.SubList.<init>(AbstractList.java:624)
        at java.util.RandomAccessSubList.<init>(AbstractList.java:775)
        at java.util.AbstractList.subList(AbstractList.java:484)
        at java.util.Vector.subList(Vector.java:1034)
        at VectorSubListIllegalArg.main(VectorSubListIllegalArg.java:18)



 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment