数组的setter和getter

ars*_*l88 1 java arrays

我是Java的新手,我需要澄清如何解决问题。

我有一个类Epicycle,定义如下:

public class Ts_epicycle {
    private double epoch;
    private double[] tle = new double[10];
}
Run Code Online (Sandbox Code Playgroud)

在另一个类中,Refine我正在调用需要tle数组的方法:

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

methodExample(keps1, keps2, e.tle);
Run Code Online (Sandbox Code Playgroud)

在methodExample中,我将为 tle

1)为tle数组创建吸气剂/吸气剂的最佳方法是什么?(以及其他变量)。

2)在methodExample中,我需要传递整个tle数组的参数,而不是它的任何特定索引。我将如何处理。

抱歉,如果我不清楚。

Joo*_*gen 5

实际上是一个有趣的问题:

为了更改获取的数组中的条目不会更改原始对象,您需要返回该数组的副本。不太好

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

    public double[] getTLE() {
        return Arrays.copyOf(tle, tle.length);
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用List类而不是数组:

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

    public List<Double> getTLE() {
        return Collections.unmodifiableList(tle);
    }
}
Run Code Online (Sandbox Code Playgroud)

这不会产生副本,但是简单地不允许在运行时更改列表。这里的效率低下是因为Double对象包装了double。

最好的方法是使用新的Stream类:遍历双打:

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

    public DoubleStream getTLE() {
        return Stream.of(tle);
    }
}
Run Code Online (Sandbox Code Playgroud)