Results 1 to 2 of 2
Thread: Trouble with creating TreeSet
- 12-27-2009, 02:05 AM #1
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
Trouble with creating TreeSet
To anyone that can point me in the right direction, I'm attempting to create a TreeSet using the Set interface with doesn't allow duplicates. However, it does allow you to just this:
Example: ts.add(John, Doe, ISBN:xxxxxxxxxxx, publisher: ToToo); then possibly retrieve the publishers names alphabetically but I received numerous errors using an iterator. My understanding is that the API list set that interfaces with a TreeSet.
My code is as follows:
I also tried to create a separate instance variable for doing this processJava Code:import java.util.Collection; import java.util.Iterator; import java.util.Set; import java.util.TreeSet; public class SetTest { private static Object itr; private Object aSet; private Object bSet; private String set; private String element; private Iterator<String> b_itr; public static void main(String args[]) { private Object bSet; private void Iterator () { //create TreeSet TreeSet< String > aSet = new TreeSet<String> aSet ); //Use iterator to display contents of TreeSet System.out.println( "Originals contents of Tree are: " ); Iterator<String> itr = bSet.iterator(); while(itr.hasNext()){ String element = itr.next(); System.out.println( element + " " ); } //end while System.out.println(); } //end private //Modify Objects to iterate Iterator<String> b_itr = bSet.Iterator(); while(b_itr.hasNext()){ String element = b_itr.next(); b_itr.set(element + "+"); } //Prints the entire list in a sorted order System.out.println( "Modified contents of bSet: " ); itr = bSet.iterator(); while(itr.hasNext)){ String str = itr.next(); System.out.print(str + " "); } System.out.println(); //Objects created Alphabetically in TreeSet listing - can ADD directly TreeSet<Set , String> cSet = new TreeSet<Set , String>(); //TreeSet ingores duplicates and is added right into the data structure cSet.add( "Johnny", "Smith", "0123456789", "HarperCollins" ); cSet.add("Freddie", "Jones", "0123456777", "HarperCollins"); cSet.add("Lilly", "Lane", "0122222234", "Penguin"); cSet.add("Bobby" , "Crosby" , "0111102345" , "Atlantic" ); cSet.add("Troy", "James", "0119233334", "Atlantic"); //Since its sorted we can ask for various subset System.out.println( "Lowest(alphabetically) is" + cSet.first()); //Print bSet greater than "k" System.out.println(cSet.tailSet("K").toArray[].length + "cSet higher than \"K\""); //Prints the entire list alphabetically System.out.println( "Sorted list: " ); java.util.Iterator - = cSet.iterator(); while(itr.hasNext()) System.out.println(itr.next()); } //end MainLast edited by Fubarable; 12-27-2009 at 02:19 AM. Reason: code tags added
-
Everything is wrong here, from your trying to have a Set that uses two generic classes (Set and String), to your attempt to use the add method to add multiple Strings to your set object . I suggest you first try a very small program using TreeSet and go from there.
Also, you need to learn to compile often and not add any new code until the code in hand compiles without errors. Otherwise you'll end up with what you have: a rat's nest of errors, and code that can't be fixed and needs to be tossed.
Similar Threads
-
Having trouble removing from TreeSet
By xtrmi in forum New To JavaReplies: 2Last Post: 05-02-2009, 04:01 AM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM -
TreeSet Demonstration
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:34 PM -
ClassCastException in TreeSet
By pHew in forum New To JavaReplies: 2Last Post: 01-16-2008, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks