Results 1 to 9 of 9
  1. #1
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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);
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default 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:

    Java Code:
    A object = new A(52, 13.0, "foo");
    If you want to pass arrays then you would need to define a constructor like so:

    Java Code:
    public A(int i, double[] d, String[] s)
    Last edited by Solarsonic; 10-25-2011 at 04:50 AM.

  3. #3
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

    Java Code:
    public A(int i, double[] d, String[] s)
    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:
    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.
    )

  4. #4
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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);

  5. #5
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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.

  6. #6
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  7. #7
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default 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).

  8. #8
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  9. #9
    Dreaming is offline Member
    Join Date
    Oct 2011
    Posts
    49
    Rep Power
    0

    Default Re: Assigning array values to objects in a constructor

    Thanks for the advice.

Similar Threads

  1. Replies: 8
    Last Post: 05-18-2011, 04:01 PM
  2. array of objects with user defined values
    By swathi dharmaraj in forum New To Java
    Replies: 3
    Last Post: 04-14-2011, 04:59 PM
  3. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:14 PM
  4. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 04:13 AM
  5. declaring fields without assigning values to them
    By diggitydoggz in forum New To Java
    Replies: 12
    Last Post: 01-03-2009, 09:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •