arsenal88
arsenal88

Reputation: 1160

Setters and getters for arrays

I am new to Java and I need some clarification how to approach an issue.

I have a class Epicycle, defined below:

public class Ts_epicycle {
    private double epoch;
    private double[] tle = new double[10];
}

In another class, Refine I am calling a method that requires the tle array:

// create an instance of Epicycle
Epicycle e = new Epicycle();

methodExample(keps1, keps2, e.tle);

In methodExample, I would be setting the array values for tle

1) What is best way for creating getters/setters for the tle array? (and for the other variable too).

2) In the methodExample, I need to pass in the argument for the whole tle array rather than any particular index of it. How would I go about this.

Apologies if i'm not making it clear.

Upvotes: 2

Views: 4863

Answers (4)

Zeuzif
Zeuzif

Reputation: 317

your array is an object like any other object in java .

// to declare and initialize your array

private int[] st = new int[10];

//getter and setter 

public int[] getSt() {
        return st;
    }
public void setSt(int[] st) {
        this.st = st;
    }

//for the last method u can use 

public void method(int value)
{
for(int i = 0 ; i<st.lenght ; i++){
st[i] += value; // for exemple
}

Upvotes: 0

davioooh
davioooh

Reputation: 24666

As a general best practice every field in a class that you need to access from another class should be provided with a getter and (if the object is intended as mutable) a setter.

As well explained by Joop Eggen in his answer is usually a good practice to return a copy or a proxy (for example a List/Collection referencing the array), in order to preserve the state of the original array.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109532

In fact an interesting question:

In order that altering entries in the gotten array does not alter the original object, you would need to return a copy of the array. Not so nice.

public class TsEpicycle {
    private double epoch;
    private double[] tle = new double[10];

    public double[] getTLE() {
        return Arrays.copyOf(tle, tle.length);
    }
}

Alternatively you could use the List class instead of an array:

public class TsEpicycle {
    private double epoch;
    private List<Double> tle = new ArrayList<>();

    public List<Double> getTLE() {
        return Collections.unmodifiableList(tle);
    }
}

This does not make a copy, but simple disallows at run-time to alter the list. Here the inefficiency is in the Double objects wrapping doubles.

The best might be to use the new Stream class: for iterating through the doubles:

public class TsEpicycle {
    private double epoch;
    private double[] tle = new double[10];

    public DoubleStream getTLE() {
        return Stream.of(tle);
    }
}

Upvotes: 5

Alex
Alex

Reputation: 722

If you want to only allow users to edit the array one at a time, then you can add the synchronized keyword to the method signature. Accessing an array is already thread safe so you don't need anything there

For example:

double getTle(int index) {
    return tle[index]
}

synchronized void setTle(int index, double value) {
    tle[index] = value;
}

This only allows the method to be called once at a time

Upvotes: 0

Related Questions