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 4 of 4

Thread: how to initialise set in a constructor

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

    Default how to initialise set in a constructor

    Hi, I need to initialise a set in the constructor. I have a set that already contains some values called "numbers" but I need to initialise this set called "newNumbers" with the same values in the constructor. The code I have tried is:

     
     public PhoneBook()
      {
         super();
         this.newNumbers = new TreeSet<Integer>();
         this.newNumbers = numbers;
    }

    im a complete novice so im probably going about this the wrong way.

    Thankyou in advance to anyone willing and able 2 help.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to initialise set in a constructor

    That's not enough information for us to figure out what is wrong. Is the variable numbers being setup in the super constructor, or is it being passed to your constructor for duplicating? Do you want to create a new set with the identical values, or do you want to use the same object and just reference it?

    Also, as a sidenote I take it newNumbers contains phone numbers, right? I know that it's not likely there is a number that starts with 0, but it is possible. A better method of storing phone numbers is with a String object.

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

    Default Re: how to initialise set in a constructor

    thankyou for your response, I want to create a new set containing the numbers that which are being passed for duplication. The numbers are just single numbers not actual phone numbers and there are no 0's..
    Last edited by davie; March 12th, 2010 at 05:00 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to initialise set in a constructor

    Ah. Ok, so what you will need to do is first of all put a parameter for your constructor that gives it the original list of numbers.

    public PhoneBook(TreeSet<Integer> numbers)

    The TreeSet class has a convenient constructor that will add all of the elements of a Collection into it in natural order (aka. sorted). Simply call that constructor, passing your numbers as a parameter and it will automatically create your new TreeSet with the numbers in it. On a side note, I'm not completely positive if it clones each element or just uses the reference, but because Integers are immutable, I don't think it matters here.

    TreeSet<Integer> newSet = new TreeSet<Integer>(numbers);

Similar Threads

  1. calling a constructor
    By turnwellm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2010, 08:46 PM
  2. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM
  3. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM