• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
A teeny tiny vulgar attempt to get you to buy our stuff
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic