The result for the code below is "ex [6, 7, b, d] [6, 7, b]". Therefore I am wondering how did the exception take place and why is the value 3 missing?

import java.util.*;

public class Exam6 {
    public static void main(String[] args){
    	TreeSet<String> t1 = new TreeSet<String>();
    	TreeSet<String> t2 = new TreeSet<String>();
    	t1.add("b");
    	t1.add("7");
    	t2 = (TreeSet)t1.subSet("5", "c");
    	try{
    		t1.add("d");
    		t2.add("6");
    		t2.add("3");
    	}catch(Exception e){
    		System.out.println("ex ");
    	}
    	System.out.println(t1 + " " + t2);
    }
}

Thank You.

Try printing the exception - it's message will help.

ps: subSet gives a "live" view, not a copy

Edited 5 Years Ago by JamesCherrill: cryptic clue ps

ps: subSet gives a "live" view, not a copy

Therefore I commented a single line of code that contain the subset method:

import java.util.*;

public class Test_2 {
    public static void main(String[] args){
    	TreeSet<String> t1 = new TreeSet<String>();
    	TreeSet<String> t2 = new TreeSet<String>();
    	t1.add("b");
    	t1.add("7");
    	//t2 = (TreeSet)t1.subSet("5", "c");
    	try{
    		t1.add("d");
    		t2.add("6");
    		t2.add("3");
    	}catch(Exception e){
    		System.out.println("ex ");
    	}
    	System.out.println(t1 + " " + t2);
    }
}

The result is [7, b, d] [3, 6] instead of ex [6, 7, b, d] [6, 7, b]. There is a huge difference in terms of the output. After I uncomment the single line of code and place the System.out.println("The size is: " + t1.size());, the output was 2. This means that the value "7,b" was copied to t2. The code below demonstrates my test result:

import java.util.*;

public class Test_2 {
    public static void main(String[] args){
    	TreeSet<String> t1 = new TreeSet<String>();
    	TreeSet<String> t2 = new TreeSet<String>();
    	t1.add("b");
    	System.out.println("T-1");
    	t1.add("7");
    	System.out.println("T-2");
    	t2 = (TreeSet)t1.subSet("5", "c");
    	System.out.println("The size is: "  + t2.size());
    	try{
    		t1.add("d");
    	    System.out.println("T-3");
    		t2.add("6");
    		System.out.println("T-4");
    		t2.add("3");
    		System.out.println("T-5");
    	}catch(Exception e){
    		System.out.println("ex ");
    		System.out.println("T-6");
    	}
    	System.out.println(t1 + " " + t2);
    }
}

Edited 5 Years Ago by solomon_13000: n/a

I don't think you read my post properly.
1. Java kindly throws you an Exception which contains a message explaining what and why. You should ALWAYS print the Exception in your catch blocks so you can see that information. It WILL help.
2. I drew your attention to the way that subset works because that's important to understanding this result. I cannot imagine why you removed that line; all that does is to change your program into a different program that nobody is interested in.

Go back to the original code. Add a System.out.println(e); to the catch clause. Read what it prints. Think about how subset works. Get a sheet of paper and write down how t1 and t2 will change after each line of your program. Then you'll understand it.

This article has been dead for over six months. Start a new discussion instead.