Results 1 to 9 of 9
- 10-25-2011, 02:40 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Assigning array values to objects in a constructor
My question is pretty simple but I couldn't find answer.
Java Code:class A{ int i; double [] d; String [] s; public A(int i, double d, String s) { this.i = i; this.d= d; this.s = s; } } class B { A a = new A(52, //Now how can I pass the double and string array arguments to the constructor); }
- 10-25-2011, 03:48 AM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Re: Assigning array values to objects in a constructor
Well you have to use the parameters provided by your constructor(s).
In this case: int, double, String.
So you could create an object like this:
If you want to pass arrays then you would need to define a constructor like so:Java Code:A object = new A(52, 13.0, "foo");
Java Code:public A(int i, double[] d, String[] s)
Last edited by Solarsonic; 10-25-2011 at 03:50 AM.
- 10-25-2011, 04:30 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
OK, first I made the mistake with the constructor, I meant to write it this way.
So if I have a constructor like and I want to instantiate A, how can I pass the d and s array arguments?Java Code:public A(int i, double[] d, String[] s)
Java Code:class A { int i; double [] d; String [] s; public A(int i, double[] d, String[] s) } class B { A a = new A(27, //Now how can I pass the array d and s arguments here? //I just can't write 23.4 and "something" because this will be double and string and not double and string arrays. )
- 10-25-2011, 05:44 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
double[] d = {1.0, 2.0, 3.0};
int[] i = {1, 2, 3};
String[] s = {"one", "two", "three"};
A a = new A(i, d, s);
- 10-25-2011, 06:09 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
Thanks, got it now. So we have to declare array before we can use them during instantiation. I was thinking something along A a = new A(2, {2.3, 4.5}, {"abc", "def"}) where you write the values directly maybe possible but it seems I got it wrong. I guess it is because since they are considered objects, they have to be declared before one can use them.
- 10-25-2011, 06:12 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
That might work...I actually don't know, but it's not very readable. I would declare in advance.
- 10-25-2011, 06:14 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
I don't think it works because I tried and it gave me an error(in Eclipse).
- 10-25-2011, 06:15 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: Assigning array values to objects in a constructor
I would also use meaningful var names, even for a simple school project like this.
double[] doubleArray...
int[] intArray
string myString
etc...
It is just good habit. Save the single letter names for small loops.
- 10-25-2011, 06:17 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 49
- Rep Power
- 0
Similar Threads
-
Doubt in assigning pixel values in MemoryImageSource
By atiprashant in forum Java 2DReplies: 8Last Post: 05-18-2011, 03:01 PM -
array of objects with user defined values
By swathi dharmaraj in forum New To JavaReplies: 3Last Post: 04-14-2011, 03:59 PM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
Assigning values to an object
By camper2 in forum New To JavaReplies: 4Last Post: 04-05-2009, 03:13 AM -
declaring fields without assigning values to them
By diggitydoggz in forum New To JavaReplies: 12Last Post: 01-03-2009, 08:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks