• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why i am getting this exception ...Can any one describe ?

 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Explorer3 {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = null;
for(int i = 606; i < 613; i++)
if(i%2 == 0) s.add(i);
System.out.println("S:-"+s);
subs = (TreeSet)s.subSet(608 , 611);

subs.add(new Integer(629)); -----------------Here i am getting the below error !!!
System.out.println(s + " " + subs);
}
}


Exception : ----


S:-[606, 608, 610, 612]
Exception in thread "main" java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$SubMap.put(TreeMap.java:891)
at java.util.TreeSet.add(TreeSet.java:210)
at dump.Explorer3.main(Explorer3.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.
 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.


In java doc the below is written :-

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Equivalent to subSet(fromElement, true, toElement, false).

*** This is not clear --> "outside its range"
what should be the range ?
 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santanu Guha wrote:
Matthew Brown wrote:Have you read the documentation for java.util.TreeSet#subSet(E, E)? It explains exactly why you get that exception.


In java doc the below is written :-

The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.

Equivalent to subSet(fromElement, true, toElement, false).

*** This is not clear --> "outside its range"
what should be the range ?


clear.resolved .....

Thanks

santanu
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the range is the range of the subset which you have set, since 629 is outside the range of subset (608 to 611, where 608 is inclusive and 611 is exclusive) thats why you are getting the exception.
 
Santanu Guha
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankush Kaundal wrote:See the range is the range of the subset which you have set, since 629 is outside the range of subset (608 to 611, where 608 is inclusive and 611 is exclusive) thats why you are getting the exception.


Thank you Ankush Kaundal very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic