Vapros otnosno toArray() na Vector...

4 отговора 139 прегледа
#1

Vapros otnosno toArray() na Vector...

Tova po-dolu e izvadka ot source na Vector zaedno s komentara kam nego. Ne razbiram kak raboti. Osobeno zashto raboti poslednia red ako razmerat na a e po-malak ot elementCount. Ako niakoi mi obiasni na prostosmarten ezik shte sam blagodaren.

* Returns an array containing all of the elements in this Vector in the
* correct order. The runtime type of the returned array is that of the
* specified array. If the Vector fits in the specified array, it is
* returned therein. Otherwise, a new array is allocated with the runtime
* type of the specified array and the size of this Vector.<p>
*
* If the Vector fits in the specified array with room to spare
* (i.e., the array has more elements than the Vector),
* the element in the array immediately following the end of the
* Vector is set to null. This is useful in determining the length
* of the Vector <em>only</em> if the caller knows that the Vector
* does not contain any null elements.
*
* @param a the array into which the elements of the Vector are to
*..be stored, if it is big enough; otherwise, a new array of t
he
* ..same runtime type is allocated for this purpose.
* @return an array containing the elements of the Vector.
* @exception ArrayStoreException the runtime type of a is not a supertype
* of the runtime type of every element in this Vector.
*/
public synchronized Object[] toArray(Object a[]) {
if (a.length < elementCount)
a = (Object[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), elementCount);


a[elementCount] = null;

}

Отговор
в отговор на Niakoi #2

Re: Vapros otnosno toArray() na Vector...

ami ako razmera na masiva e po-malyk ot broia na elementite se syzdava nov masiv i se raboti s nego..

Отговор
в отговор на Niakoi #3

Re: Vapros otnosno toArray() na Vector...

Malko po-podrobno:
Ako broq na elemetnite wyw Vector-a (elementcount) e po-golqm ot razmera na masiwa, w koito iskash da go napyhash (a.length), to se syzdawa now masiw s golemina elementcount
{java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), elementCount); }
i se setwa posledniq mu element da e null.
Ako masiwa , w koito iskash da napyhash Vector-a e po-golqm, to se pyha dokydeto move i sled posledniq napyhan element se slaga null.

Отговор
в отговор на jdp #4

Re: Oooooooooooooo...

Abe ia vijte po-vnimatelno source!!!

Kakvo znachi a[elementCount] pri polojenie, che imate elementCount elementa v masiva? S drugi dumi imate elementite ot a[0] do a[elementCount-1].

Vijte vnimatelno source i ako mojete mi otgovorete!!!

Mersi...

Отговор
в отговор на Niakoi #5

Re: Oooooooooooooo...


/**
* Returns an array containing all of the elements in this Vector in the
* correct order. The runtime type of the returned array is that of the
* specified array. If the Vector fits in the specified array, it is
* returned therein. Otherwise, a new array is allocated with the runtime
* type of the specified array and the size of this Vector.<p>
*
* If the Vector fits in the specified array with room to spare
* (i.e., the array has more elements than the Vector),
* the element in the array immediately following the end of the
* Vector is set to null. This is useful in determining the length
* of the Vector <em>only</em> if the caller knows that the Vector
* does not contain any null elements.
*
* @param a the array into which the elements of the Vector are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of the Vector.
* @exception ArrayStoreException the runtime type of a is not a supertype
* of the runtime type of every element in this Vector.
*/
public synchronized Object[] toArray(Object a[]) {
if (a.length < elementCount)
a = (Object[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), elementCount);

System.arraycopy(elementData, 0, a, 0, elementCount);

if (a.length > elementCount)
a[elementCount] = null;

return a;
}

ei towa e ot moq src.jar na JDK 1.3.1_02 .. procheti wnimatelno javadoc-a i to tam si e kazano .. :))

Отговор