Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   arrays and cloning, where is it described? (http://www.velocityreviews.com/forums/t596219-arrays-and-cloning-where-is-it-described.html)

Andreas Leitgeb 03-05-2008 11:58 AM

arrays and cloning, where is it described?
 
It seems to be quite a basic thing, but I found myself
unsure about how it works, and where it is described.
(I didn't find it in JLS nor in the description of
Cloneable or Object.clone() in the javadoc)

According to JLS, arrays are Serializable and Cloneable,
and for multidimensional arrays, the JLS says:
" A clone of a multidimensional array is shallow,
" which is to say that it creates only a single
" new array. Subarrays are shared..
I'm not sure if this should be understood to imply
shallowness also in my case. Why would it have to
explicitly mention multidimensional arrays otherwise?

If I want to deep-copy an array of some Cloneable class,
what would be the best way to do it?

I'd be glad about answers, but even more I'd be glad
about pointers to any of sun's java-documents (jls,
api), where this behaviour or some "deepCopy" method
for arrays of cloneable types is directly described.

Is array's clone/copy altogether the wrong way, and
do I need to create a new array and fill it with
clones of each element, myself?

PS: In my task at hand I do not need to worry about
compiletime/runtime-type: they're the same.


Sanny 03-05-2008 12:35 PM

Re: arrays and cloning, where is it described?
 
> According to JLS, arrays are Serializable and Cloneable,
> and for multidimensional arrays, the JLS says:
> * " A clone of a multidimensional array is shallow,
> * " * which is to say that it creates only a single
> * " * new array. Subarrays are shared..
> I'm not sure if this should be understood to imply
> shallowness also in my case. Why would it have to
> explicitly mention multidimensional arrays otherwise?



I tried to copy Single dimensional Array and it works correctly. For
multidimensional Array it do not copy all elements as you think.

int[] array1 = new int[100];
int[] source = new int[100];

array1=source.clone() Works correctly.


int[][] array1 = new int[100][100];
int[][] source = new int[100][100];

array1=source.clone() Does not Copy all elements correctly.

Bye
Sanny

Looking for Experts: http://www.GetClub.com/Experts.php


Patricia Shanahan 03-05-2008 02:45 PM

Re: arrays and cloning, where is it described?
 
Andreas Leitgeb wrote:
> It seems to be quite a basic thing, but I found myself
> unsure about how it works, and where it is described.
> (I didn't find it in JLS nor in the description of
> Cloneable or Object.clone() in the javadoc)
>
> According to JLS, arrays are Serializable and Cloneable,
> and for multidimensional arrays, the JLS says:
> " A clone of a multidimensional array is shallow,
> " which is to say that it creates only a single
> " new array. Subarrays are shared..
> I'm not sure if this should be understood to imply
> shallowness also in my case. Why would it have to
> explicitly mention multidimensional arrays otherwise?
>
> If I want to deep-copy an array of some Cloneable class,
> what would be the best way to do it?
>
> I'd be glad about answers, but even more I'd be glad
> about pointers to any of sun's java-documents (jls,
> api), where this behaviour or some "deepCopy" method
> for arrays of cloneable types is directly described.
>
> Is array's clone/copy altogether the wrong way, and
> do I need to create a new array and fill it with
> clones of each element, myself?
>
> PS: In my task at hand I do not need to worry about
> compiletime/runtime-type: they're the same.
>


The key to understanding this is to accept that Java does not really
have multidimensional arrays. It does have arrays whose elements are of
another array type, and sometimes pretends they are multidimensional
arrays, but not to the extent of making one work consistently as though
it were a single array with multiple dimensions.

Thus "new int[3][4]" is a three element array. Each element is a
reference to a four element array of int.

Cloning it creates a new three element array. Element n of the new array
refers to the same int[4] as element n of the original.

Patricia

Andreas Leitgeb 03-05-2008 05:38 PM

Re: arrays and cloning, where is it described?
 
Patricia Shanahan <pats@acm.org> wrote:
> Andreas Leitgeb wrote:
>> According to JLS, arrays are Serializable and Cloneable,
>> and for multidimensional arrays, the JLS says:
>> " A clone of a multidimensional array is shallow,
>> " which is to say that it creates only a single
>> " new array. Subarrays are shared..
>> I'm not sure if this should be understood to imply
>> shallowness also in my case. Why would it have to
>> explicitly mention multidimensional arrays otherwise?

>
> The key to understanding this is to accept that Java does not really
> have multidimensional arrays....


Sorry for obviously being higly misunderstandable.

My problem isn't about arrays of primitive types.
Not even really "multidimensional" ones.

The docu says: (in other words)
if you clone an "array of array-types" (which are,
btw., also Cloneable) then the referenced arrays
are *not* cloned.
So far so good.

While it almost seems like implied, it doesn't say:
if you clone an array of *any Object-type*,
(which would effectively include array-types)
then only the references, not the objects are
cloned.

Why does it say so specifically about arrays of
arrays, but not about arrays of *any* objects?

Is it, because people might otherwise think that
"arrays of arrays" would be more likely to be deep-
cloned than arrays of other objects?


Patricia Shanahan 03-05-2008 05:43 PM

Re: arrays and cloning, where is it described?
 
Andreas Leitgeb wrote:
....
> Why does it say so specifically about arrays of
> arrays, but not about arrays of *any* objects?
>
> Is it, because people might otherwise think that
> "arrays of arrays" would be more likely to be deep-
> cloned than arrays of other objects?
>


I suspect so. Java pretends just enough multidimensional array support
to be confusing.

Patricia

Daniel Pitts 03-06-2008 05:35 AM

Re: arrays and cloning, where is it described?
 
Patricia Shanahan wrote:
> Andreas Leitgeb wrote:
> ....
>> Why does it say so specifically about arrays of
>> arrays, but not about arrays of *any* objects?
>>
>> Is it, because people might otherwise think that
>> "arrays of arrays" would be more likely to be deep-
>> cloned than arrays of other objects?
>>

>
> I suspect so. Java pretends just enough multidimensional array support
> to be confusing.
>
> Patricia

I think it also makes a mention because array references are special.
Even though they can be assigned to an Object reference, they aren't
really an object. The JLS has to be specific about anything that might
be ambiguous otherwise.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Mike Schilling 03-06-2008 07:21 AM

Re: arrays and cloning, where is it described?
 
Daniel Pitts wrote:
> Patricia Shanahan wrote:
>> Andreas Leitgeb wrote:
>> ....
>>> Why does it say so specifically about arrays of
>>> arrays, but not about arrays of *any* objects?
>>>
>>> Is it, because people might otherwise think that
>>> "arrays of arrays" would be more likely to be deep-
>>> cloned than arrays of other objects?
>>>

>>
>> I suspect so. Java pretends just enough multidimensional array
>> support to be confusing.
>>
>> Patricia

> I think it also makes a mention because array references are
> special.
> Even though they can be assigned to an Object reference, they aren't
> really an object. The JLS has to be specific about anything that
> might be ambiguous otherwise.


In what ways are arrays not objects?



Andreas Leitgeb 03-06-2008 07:34 AM

Re: arrays and cloning, where is it described?
 
Mike Schilling <mscottschilling@hotmail.com> wrote:
> Daniel Pitts wrote:
>> Patricia Shanahan wrote:
>>> Andreas Leitgeb wrote:
>>>> Why does it say so specifically about arrays of
>>>> arrays, but not about arrays of *any* objects?
>>>> Is it, because people might otherwise think that
>>>> "arrays of arrays" would be more likely to be deep-
>>>> cloned than arrays of other objects?
>>> I suspect so. Java pretends just enough multidimensional array
>>> support to be confusing.

>> I think it also makes a mention because array references are
>> special.
>> Even though they can be assigned to an Object reference, they aren't
>> really an object. The JLS has to be specific about anything that
>> might be ambiguous otherwise.


But then it is still unspecific about cloning arrays of Objects.
....or I've just still failed to find that mentioned.

> In what ways are arrays not objects?


Don't know, if that's what Daniel meant, but arrays are not
accessed through get-/putfield (except for length) or invoke...
opcodes, but through <x>aload/<x>astore opcodes for <x> any
of [abcdfils].

The JLS has some strange wording, such as "can be assigned to
variables of types Object, Cloneable, Serializable", instead
of saying that an array *was* all these.


Lew 03-06-2008 08:16 AM

Re: arrays and cloning, where is it described?
 
Andreas Leitgeb wrote:
> The JLS has some strange wording, such as "can be assigned to
> variables of types Object, Cloneable, Serializable", instead
> of saying that an array *was* all these.


Actually, the JLS does say that an array is all these, in ss. 4.10.3
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.3>
and in 10.7
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.7>
> Every array implements the interfaces Cloneable and java.io.Serializable.

and 10.8
> The direct superclass of an array type is Object.
> Every array type implements the interfaces Cloneable and java.io.Serializable.


The combination of Cloneable and Serializable is somewhat rare outside arrays,
because Cloneable is rare. The combination is implemented by various
java.util classes: ArrayList, Calendar, Date, HashMap, HashSet.

Also in java.sql: Date, Time, Timestamp, by dint of subclassing java.util.Date.

There are a number of additional references in the JLS to the relationship
between Cloneable & Serializable and arrays such as ss. 5.1.6 (Narrowing
Reference Conversion), 5.5 (Casting Conversion), 10 (Arrays), and 5.2
(Assignment Conversion), from whence your paraphrase seems to come.

<http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.2>
> An array can be assigned only to a variable of a compatible array type,
> or to a variable of type Object, Cloneable or java.io.Serializable.


That quote makes sense there because it has to do with assignment conversion.

It is also clearly not instead of saying that an array is "all these" but in
addition to several such mentions.

--
Lew

Daniel Pitts 03-06-2008 03:19 PM

Re: arrays and cloning, where is it described?
 
Andreas Leitgeb wrote:
> Mike Schilling <mscottschilling@hotmail.com> wrote:
>> Daniel Pitts wrote:
>>> Patricia Shanahan wrote:
>>>> Andreas Leitgeb wrote:
>>>>> Why does it say so specifically about arrays of
>>>>> arrays, but not about arrays of *any* objects?
>>>>> Is it, because people might otherwise think that
>>>>> "arrays of arrays" would be more likely to be deep-
>>>>> cloned than arrays of other objects?
>>>> I suspect so. Java pretends just enough multidimensional array
>>>> support to be confusing.
>>> I think it also makes a mention because array references are
>>> special.
>>> Even though they can be assigned to an Object reference, they aren't
>>> really an object. The JLS has to be specific about anything that
>>> might be ambiguous otherwise.

>
> But then it is still unspecific about cloning arrays of Objects.
> ....or I've just still failed to find that mentioned.
>
>> In what ways are arrays not objects?

>
> Don't know, if that's what Daniel meant, but arrays are not
> accessed through get-/putfield (except for length) or invoke...
> opcodes, but through <x>aload/<x>astore opcodes for <x> any
> of [abcdfils].

Actually, "length", is not a field on an array "object". To access the
length of an array, you use a specific opcode.
<http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc.html>
> arraylength
> Description
>
> The arrayref must be of type reference and must refer to an array. It is popped from the operand stack. The length of the array it references is determined. That length is pushed onto the operand stack as an int.



>
> The JLS has some strange wording, such as "can be assigned to
> variables of types Object, Cloneable, Serializable", instead
> of saying that an array *was* all these.
>

That strange wording is *because* an array is not actually an Object.
Programmers are "tricked" into thinking it is, because it can be
assigned to an Object/Cloneable/Serializable reference.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


All times are GMT. The time now is 11:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.