Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to create a sorted set from the contents of an array

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default How to create a sorted set from the contents of an array

    Hello people,

    I need to write a public class method that has an array of integers as its argument and returns a sorted
    set containing all of the elements of that array.
    Im really stuck on this but my best attempt is as follows but this returns errors already when i Try to compile.
     
      public SortedSet arrayToSortedSet(Integer[] anArray)
      {
         Set<Integer>aSet =new TreeSet<Integer>();
         aSet.add(anArray);
         return aSet;
       }

    Sorry this is such a noob question but I just cant work out how to do it.
    Last edited by davie; March 11th, 2010 at 02:45 PM.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: How to create a sorted set from the contents of an array

    Well, this works, although there may be a more elegant solution.

    Array is not a Collection, so you cannot use TreeSet's "addAll" or constructor to include elements. Sorry... you'll have to loop the good old-fashioned way, adding one element at a time.

    Next, aSet must be declared to be of type SortedSet or TreeSet. Set won't work because you need to return a SortedSet
    and SortedSet is a subclass of Set. So yeah, the following code should work...

           public SortedSet arrayToSortedSet(Integer[] anArray)
          {
             SortedSet<Integer>aSet = new TreeSet<Integer>();
     
             for(int i = 0; i < anArray.length; i++)
                aSet.add(anArray[i]);
     
             return aSet;
          }

    A perhaps more elegant solution is to find some type of collection that will allow you to convert arrays, and then use that collection with the treeset. Personally, I think this is alright.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  3. The Following User Says Thank You to CT&SC For This Useful Post:

    davie (March 11th, 2010)

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM
  3. Merge 2 Sorted Text Files into a Third
    By Epyllion in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 10th, 2010, 08:24 PM
  4. Convert contents of JTextArea / JEditorPane to PDF
    By rangarajank in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 30th, 2009, 02:38 PM
  5. how to create exe jar
    By ttsdinesh in forum Java Theory & Questions
    Replies: 1
    Last Post: September 27th, 2009, 08:21 AM