我正在嘗試用爪哇腳本制作一個游戲。游戲板初始化為零填充的 2-D 數(shù)組。但是,當(dāng)我設(shè)置單個點的值時,將使用該值設(shè)置完整的列。我認(rèn)為這是我初始化數(shù)組的方式的一些問題。方法 1# initializationgameState = Array(6).fill(Array(7).fill(0))# later in the gamegameState[2][4] = 1# results in complete 4th index column to be assigned the value 1, like so -0: (7) [0, 0, 0, 0, 1, 0, 0]1: (7) [0, 0, 0, 0, 1, 0, 0]2: (7) [0, 0, 0, 0, 1, 0, 0]3: (7) [0, 0, 0, 0, 1, 0, 0]4: (7) [0, 0, 0, 0, 1, 0, 0]5: (7) [0, 0, 0, 0, 1, 0, 0]方法 2# initializationlet gameState = [];for (let i=0; i<MAX_ROWS; i++) { let row = [] for (let j=0; j<MAX_COLUMNS; j++) { row.push(0) } gameState.push(row);}# again similar assignmentgameState[2][4] = 1# results in correct state of the array0: (7) [0, 0, 0, 0, 0, 0, 0]1: (7) [0, 0, 0, 0, 0, 0, 0]2: (7) [0, 0, 0, 0, 1, 0, 0]3: (7) [0, 0, 0, 0, 0, 0, 0]4: (7) [0, 0, 0, 0, 0, 0, 0]5: (7) [0, 0, 0, 0, 0, 0, 0]有人可以解釋我在這里做錯了什么嗎?
Javascript 多維數(shù)組完成列獲取集
月關(guān)寶盒
2022-09-11 20:14:43