0

I got a constructor that starts like this:

public Unit(String name, double[] initialPosition, int weight, int strength, int agility, int toughness, 
        int currentHealth, int currentStamina) {

I want to write out some tests, but to do that I need to know the syntax to pass an array to the constructor. I looking for a way to do this without having to define the array before I call the constructor.

1
  • 2
    If you don't want to define the array before you call the constructor, then why make it a parameter? You could always overload the constructor -- Or create another constructor that doesn't use the array as a parameter. Commented Feb 29, 2016 at 14:07

3 Answers 3

3

Either create the array when calling the constructor (inline):

new Unit("myname", new double[]{1.0,2.0},...);

or restructure your constructor to use varargs:

public Unit(String name, int weight, int strength, int agility, int toughness, 
    int currentHealth, int currentStamina, double... initialPosition) { ... }

//call
new Unit("myname", w,s,a,t,c,stam, 1.0, 2.0 );

However, I assume you need a specific number of coordinates for the position so I'd not use an array but an object for that:

class Position {
  double x;
  double y;

  Position( x, y ) {
    this.x = x;
    this.y = y;
  }
}

public Unit(String name, Position initialPosition, int weight, int strength, int agility, int toughness, 
    int currentHealth, int currentStamina ) { ... }

//call:
new Unit( "myname", new Position(1.0, 2.0), ... );

Advantages over using an array:

  • It is typesafe, i.e. you pass in positions and not some arbitrary array of doubles. This prevents bugs where you accidentially pass in some other array.
  • It defines the number of coordinates at compiletime, i.e. you know the number of coordinate a position has (2 in my example) whereas when using an array (or varargs which is basically the same) you could pass any number of coordinates (0 to Integer.MAX_VALUE).
Sign up to request clarification or add additional context in comments.

Comments

1

You can use inline parameters when calling the Unit constructor...

Example:

Unit(String name, double[] initialPosition, int weight, int strength, int agility, int toughness, 
        int currentHealth, int currentStamina) {

will be

Unit("String name", new double[]{0.0, 1.1, 3.3}, 0, 3, 2, 1, 
        2, 4) {

Does this look like what you need???

1 Comment

thank you for the answer, but I accepted Thomas' answer because it says the same but gives more options as well
0

When you pass an array to any method or constructor, the value of it's reference is passed. Reference means address..

An example: Class: Unit

Double carray[]; //class variable (array)
Unit(Double[] array) //constructor
{
    this.carray=array;
    this.carray={3.145,4.12345.....};
     //passing an array means, you are actually passing the value of it's reference.
//In this case, `carray` of the object ob points to the same reference as the one passed
}

public static void main(String[] args)
{
    Double[] arr=new Double[5];
    Unit ob=new Unit(arr);
     //passes `reference` or `address` of arr to the constructor.
}

2 Comments

This doesn't fit "I looking for a way to do this without having to define the array before I call the constructor."
That doesn't make it better IMO. You now define an array of 5 elements which all are null. I guess the OP wants to pass in some values instead of creating them in the constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.