> public MyClass
> {
[quoted text clipped - 3 lines]
> {
> _dataArray = inputArray;
_dataArray = inputArray.clone();
> }
>
> public double[] getDataArray()
> {
> return _dataArray; //privacy leak
return _dataArray.clone();
> }
>
> }
>
> An Eclipse plugin pointed out that both places could be causing privacy
> leak. [...]
If it was a List<Double> instead of double[], then use new
ArrayList<Double>(data). Collections.unmodifiableList is also a
possibility for outputs, but the unodifiable list tracks the original list.
Tom Hawtin
www - 19 Apr 2007 19:51 GMT
>> public MyClass
>> {
[quoted text clipped - 13 lines]
>>
>> }
I just checked my Java text book. Here is the answer:
public double[] getDataArray()
{
double temp = new double[_dataArray.length];
for(int i=0; i<_dataArray.length; i++)
{
temp[i] = _dataArray[i];
}
return temp;
}
I am wondering which way is better, comparing to your way (return
_dataArray.clone() ). Is your way a new feature in Java?
Tom Hawtin - 19 Apr 2007 20:46 GMT
> I just checked my Java text book. Here is the answer:
> [...]
> double temp = new double[_dataArray.length];
^[]
> I am wondering which way is better, comparing to your way (return
> _dataArray.clone() ). Is your way a new feature in Java?
No, it's been in there since 1.0. However for 1.4 and earlier you had to
write:
return (double[])_dataArray.clone();
To explicitly write a loop to copy array is nuts.
Tom Hawtin
Eric Sosman - 19 Apr 2007 21:26 GMT
Tom Hawtin wrote On 04/19/07 15:46,:
>>I just checked my Java text book. Here is the answer:
>>[...]
[quoted text clipped - 11 lines]
>
> To explicitly write a loop to copy array is nuts.
Agreed: In tests discussed on this newsgroup at the
end of February and beginning of March, new-and-loop took
about 30% longer than clone(), and almost twice as long
as new-and-arrayCopy() ...
YMMV.

Signature
Eric.Sosman@sun.com