<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Howtos &#187; array</title>
	<atom:link href="http://howto.isgoodness.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://howto.isgoodness.com</link>
	<description>Stuffs that are worth to mention and worth to know</description>
	<lastBuildDate>Sun, 27 Feb 2011 16:19:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ReferenceError #1065: can not create property 0 on &#8230; (and #1069)</title>
		<link>http://howto.isgoodness.com/2010/02/referenceerror-1065-can-not-create-property-0-on-and-1069/</link>
		<comments>http://howto.isgoodness.com/2010/02/referenceerror-1065-can-not-create-property-0-on-and-1069/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 13:20:44 +0000</pubDate>
		<dc:creator>Van Nhu</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[dynamic class]]></category>
		<category><![CDATA[reference error]]></category>
		<category><![CDATA[shuffle]]></category>

		<guid isPermaLink="false">http://howto.isgoodness.com/?p=422</guid>
		<description><![CDATA[Last week, I was trying to create a shuffle function on my array class which extends the native Array class. I got the ReferenceError when running the code bellow. The reason is that Array is a dynamic class and my is not. Since Array is dynamic, it will try to create new property when you [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, I was trying to create a <a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle">shuffle </a>function on my array class which extends  the native Array class. I got the ReferenceError when running the code bellow. The reason is that Array is a dynamic class and my is not.  Since Array is dynamic, it will try to create new property when you try to assign some value to a nonexistent instance variable but MyArray prevents this. If you try this you will see that reference error #1065 is occurred when the array is constructed. If you remove the constructor you will get reference error #1069 instead and it hapens when you try to swap the elements in shuffle method (trying to access a nonexistent instance variable). So make sure that you declare dynamic if you extend a dynamic class or better if editor or compiler gives a warning if you forget &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">&nbsp;
<span style="color: #9900cc; font-weight: bold;">package</span> vnmedia.common.lang
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> vnmedia.common.lang.math.Random;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyArray extends <span style="color: #004993;">Array</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyArray<span style="color: #000000;">&#40;</span>...<span style="color: #004993;">parameters</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">parameters</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> shuffle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000000; font-weight: bold;">:*</span>, k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, r<span style="color: #000000; font-weight: bold;">:</span>Random = <span style="color: #0033ff; font-weight: bold;">new</span> Random<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i = <span style="color: #0033ff; font-weight: bold;">this</span>.<span style="color: #004993;">length</span>; i <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				<span style="color: #009900;">// 0 &lt;= k &lt;= i - 1</span>
				k = r.nextInt<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;
				a = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
				<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = a;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>However, I think the best way to provide a shuffle function for arrays is  to define a static ArrayUtil.shuffle as bellow</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> vnmedia.common.lang
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> vnmedia.common.lang.math.Random;
&nbsp;
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ArrayUtil
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ArrayUtil<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> shuffle<span style="color: #000000;">&#40;</span>arr<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> a<span style="color: #000000; font-weight: bold;">:*</span>, k<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, r<span style="color: #000000; font-weight: bold;">:</span>Random = <span style="color: #0033ff; font-weight: bold;">new</span> Random<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i = arr.<span style="color: #004993;">length</span>; i <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span>; i<span style="color: #000000; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
				k = r.nextInt<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;
				a = arr<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span>;
				arr<span style="color: #000000;">&#91;</span>k<span style="color: #000000;">&#93;</span> = arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>;
				arr<span style="color: #000000;">&#91;</span>i<span style="color: #000000; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = a;
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://howto.isgoodness.com/2010/02/referenceerror-1065-can-not-create-property-0-on-and-1069/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

