Go Back   Java Programming Forums > Java Standard Edition Programming Help > Collections and Generics


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2010, 07:54 PM
Junior Member
 

Join Date: Nov 2009
Posts: 9
Thanks: 5
Thanked 1 Time in 1 Post
davie is on a distinguished road
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:

Java Code
 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.



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 12-03-2010, 08:19 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 12-03-2010, 08:27 PM
Junior Member
 

Join Date: Nov 2009
Posts: 9
Thanks: 5
Thanked 1 Time in 1 Post
davie is on a distinguished road
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; 12-03-2010 at 09:00 PM.
Reply With Quote
  #4 (permalink)  
Old 12-03-2010, 09:35 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.

Java Code
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.

Java Code
TreeSet<Integer> newSet = new TreeSet<Integer>(numbers);
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a constructor turnwellm What's Wrong With My Code? 1 04-03-2010 12:46 AM
Declaring variables in constructor and compiling Newoor Object Oriented Programming 3 05-12-2009 05:07 PM
Private Constructor Ganezan Object Oriented Programming 4 07-11-2009 08:02 PM
error cannot find symbol-constructor Person(java.lang.String,java.lang.String,Date) ss7 What's Wrong With My Code? 2 03-11-2009 10:28 AM
Passing objects as a constructor parameter derky Object Oriented Programming 2 27-10-2009 08:31 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:52 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.