2015-05-05 175 views
0

我想創建2d數組,但是我得到錯誤。這裏是我的代碼:如何動態創建二維數組?

var cleanArr:Array = new Array(); 
for (var i:int = 0; i < arr.length; i++) 
{ 
    cleanArr[i][0] = arr[i].substring(0, 29); 
    cleanArr[i][1] = arr[i].substring(29, int.MAX_VALUE); 

    trace(cleanArr[i]); 
} 

我得到這個錯誤:

TypeError: Error #1010: A term is undefined and has no properties.

at SubtitleLoader/onComplete()[C:\Users\ ... \SubtitleLoader.as:88]

at flash.events::EventDispatcher/dispatchEventFunction()

at flash.events::EventDispatcher/dispatchEvent()

at flash.net::URLLoader/onComplete()

線88:cleanArr [I] [0] =改編[I] .substring(0,29);

我該如何解決這個問題?

回答

2

cleanArr是完全空的,它不是2D數組。對於它是一個二維數組,其中的項目必須是數組。在你的代碼中,它們根本不存在。你必須先在給定的位置創建一個數組。試試這個:

var cleanArr:Array = new Array(); 
for (var i:int = 0; i < arr.length; i++) 
{ 
    cleanArr[i] = new Array(); 
    cleanArr[i][0] = arr[i].substring(0, 29); 
    cleanArr[i][1] = arr[i].substring(29, int.MAX_VALUE); 

    trace(cleanArr[i]); 
} 
+0

謝謝你的回答。這工作正常。 – nikel