简体   繁体   中英

Passing a list of doubles as array to a function that expects a double array - Java

I am trying to create a Planet class where I want to set the velocity, acceleration, location of a planet to a default value. I have setter function to do that but they expect a double array. It'd be extremely convenient to pass a list of numbers as arguments since I don't want to create an array of zeroes each time I initialize a new planet.

Well, to sum up I would like to do something like this by default:

    public Planet(){
        dimension = 3;
        mass = 1;
        acceleration = new double[dimension];
        velocity = new double[dimension];
        location = new double[dimension];
        this.setLocation({0,0,0}); // Is this possible?
        this.setVelocity({0,0,0}); // ?
        this.setAcceleration({0,0,0}); // ?
    }

Where my setters look like this:

    public void setVelocity(double[] velocity){
        for(int i = 0; i < dimension; i++){
            this.velocity[i] = velocity[i];
        }
    }

I am fairly new to Java and probably this is possible but I couldn't find the solution by myself. In C++ this is possible by initializer lists but a Google search suggested that Java does not have one.

you can pass in an anonymous array of doubles by using the syntax:

new double[]{...} 

in your case it would be something like:

this.setLocation(new double[]{0,0,0});
this.setVelocity(new double[]{0,0,0}); 
this.setAcceleration(new double[]{0,0,0}); 

In Java all arrays are preinitialized (in contrast to C/C++), so when you create array of doubles

velocity = new double[dimension];

you already get it initializedd with the following values

{ 0.0, 0.0, 0.0, ... }

If you need some other value assigned to every element you can use Arrays#fill() method.

I would suggest you bypass the setter and include this line in your default Planet() constructor:

this.acceleration = new double[3] { 0.0, 0.0, 0.0 };

You can do this for all 3 arrays.

Arguably, this is a clean way to see what you get when calling Planet() without having to jump to the examine the setter methods for each array.

I've implemented your class with some addition changes:

public class Planet {
    private final int dimension;
    private final int mass = 1;
    private double[] acceleration;
    private double[] velocity;
    private double[] location;

    public Planet(//You can pass all of these fields here) {
        this.dimension = 3;
        this.mass = 1;
        this.setLocation(new double[]{0, 0, 0});
        this.setVelocity(new double[]{0, 0, 0});
        this.setAcceleration(new double[]{0, 0, 0});
    }

    public void setAcceleration(double[] acceleration) {
        this.acceleration = acceleration;
    }

    public void setVelocity(double[] velocity) {
        this.velocity = velocity;
    }

    public void setLocation(double[] location) {
        this.location = location;
    }
}

You tell that you don't want to "create" (initialize) arrays inside the constructor. But you have to initialize them at least one time anyway for example you can use other way:

private double[] acceleration = new double[dimension];
private double[] velocity = new double[dimension];
private double[] location = new double[dimension];

And then you add values to these arrays in the constructor. But this variant is possible only if dimension is initialized before, so it depends on your design. More likely dimension supposed to be constant so you can initialize it like:

private static final int DIMENSION = 3;

And you can use the second variant. Also you can rid of setters if you need them only within the constructor. Then simply:

for(int i = 0; i < DIMENSION; i++) {
    acceleration[i] = 0; //or some other number passed to the constructor
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM