Friday 4 April 2014

set : Replace element at particular index

E set(int index, E element)
   Replaces the element at the specified position in this list with the specified element. Return the element previously at the specified position

import java.util.*;
class ListSet{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  /* Replace the Elements */
  for(int i=0; i < myList.size(); i++){
   System.out.println("Current Element At " + i +"th Position is " + myList.set(i, i));
   System.out.println("After Replacing " + i + "th element ");
   System.out.println(myList);
  }
 }
}


Output
Elements in the List are
[10, 20, 30, 40]
Current Element At 0th Position is 10
After Replacing 0th element
[0, 20, 30, 40]
Current Element At 1th Position is 20
After Replacing 1th element
[0, 1, 30, 40]
Current Element At 2th Position is 30
After Replacing 2th element
[0, 1, 2, 40]
Current Element At 3th Position is 40
After Replacing 3th element
[0, 1, 2, 3]

1. throws UnsupportedOperationException if the set operation is not supported by this list

import java.util.*;
class ListSetUnsupport{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  List<Integer> unmodifiable;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  unmodifiable = Collections.unmodifiableList(myList);
  System.out.println("Trying to Replace the Element in unmodifiable List");
  unmodifiable.set(0, 100);  
 }
}
 

Output
Elements in the List are
[10, 20, 30, 40]
Trying to Replace the Element in unmodifiable List
Exception in thread "main" java.lang.UnsupportedOperationException
        at java.util.Collections$UnmodifiableList.set(Unknown Source)
        at ListSetUnsupport.main(ListSetUnsupport.java:18)

2. throws ClassCastException if the class of the specified element prevents it from being added to this list

3. throws NullPointerException if the specified element is null and
this list does not permit null elements

4. throws IllegalArgumentException if some property of the specified
element prevents it from being added to this list

5. throws IndexOutOfBoundsException if the index is out of range
import java.util.*;
class ListSetIndexOut{
 public static void main(String args[]){
  List<Integer> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  System.out.println("Elements in the List are");
  System.out.println(myList);
  
  System.out.println("Trying to Replace the Element which is out of Bounds");
  myList.set(5, 100);  
 }
}

Output
Elements in the List are
[10, 20, 30, 40]
Trying to Replace the Element which is out of Bounds
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5, Size:4
        at java.util.ArrayList.rangeCheck(Unknown Source)
        at java.util.ArrayList.set(Unknown Source)
        at ListSetIndexOut.main(ListSetIndexOut.java:16)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment