|
Author |
Topic: Mutli-Dimensional arrays (Read 292 times) |
|
jhv2
|
Mutli-Dimensional arrays
« on: Feb 25th, 2004, 3:37am » |
|
Hi all, Thanks for fielding these questions, it's a great resource for students. After reading the "arrays" right-up in the reference, I tried the below as a two-dimensional array. The error I get complains about syntax (unexpected token: { ) but I just can't figure out why. Is this easily explained? (thanks) Jason (a sample of the offending material) <CODE> int MatTower[][] = new int [13][8]; MatTower[0] = {0,0,0,0,0,0,0,0}; MatTower[1] = {3,3,6,3,0,0,0,0}; MatTower[2] = {3,3,9,0,0,0,0,0}; MatTower[3] = {6,3,3,3,3,3,3,6}; MatTower[4] = {9,0,3,3,3,3,0,9}; MatTower[5] = {3,3,3,3,3,3,0,12}; MatTower[6] = {0,20,0,3,0,0,0,0}; MatTower[7] = {15,3,0,3,0,3,0,20}; MatTower[8] = {0,0,0,12,0,0,0,0}; MatTower[9] = {0,12,0,0,0,0,0,12}; MatTower[10] = {0,8,8,0,0,0,0,8}; MatTower[11] = {0,30,0,0,0,0,0,0}; MatTower[12] = {12,0,0,12,0,0,0,12}; </CODE>
|
|
|
|
Mythmon
|
Re: Mutli-Dimensional arrays
« Reply #1 on: Feb 25th, 2004, 3:46am » |
|
im not _sure_ but i think that the only time you can use {}'s to assign to arrays is on creation, so try and add all those either at creation (is that possible?) or try to assign them all either seperatly or in a loop, if you can
|
|
|
|
benelek
|
Re: Mutli-Dimensional arrays
« Reply #2 on: Feb 25th, 2004, 7:41am » |
|
Yeah, i think Mythmon is right. Here's how you could do it: Code: int[][] MatTower = { {0,0,0,0,0,0,0,0}, {3,3,6,3,0,0,0,0}, {3,3,9,0,0,0,0,0}, {6,3,3,3,3,3,3,6}, {9,0,3,3,3,3,0,9}, {3,3,3,3,3,3,0,12}, {0,20,0,3,0,0,0,0}, {15,3,0,3,0,3,0,20}, {0,0,0,12,0,0,0,0}, {0,12,0,0,0,0,0,12}, {0,8,8,0,0,0,0,8}, {0,30,0,0,0,0,0,0}, {12,0,0,12,0,0,0,12} }; |
|
|
|
|
|
TomC
|
Re: Mutli-Dimensional arrays
« Reply #3 on: Feb 25th, 2004, 11:00am » |
|
You could do it like this, too, I think... Code: int MatTower[][] = new int [13][8]; MatTower[0] = new int[]{0,0,0,0,0,0,0,0}; MatTower[1] = new int[]{3,3,6,3,0,0,0,0}; MatTower[2] = new int[]{3,3,9,0,0,0,0,0}; MatTower[3] = new int[]{6,3,3,3,3,3,3,6}; MatTower[4] = new int[]{9,0,3,3,3,3,0,9}; MatTower[5] = new int[]{3,3,3,3,3,3,0,12}; MatTower[6] = new int[]{0,20,0,3,0,0,0,0}; MatTower[7] = new int[]{15,3,0,3,0,3,0,20}; MatTower[8] = new int[]{0,0,0,12,0,0,0,0}; MatTower[9] = new int[]{0,12,0,0,0,0,0,12}; MatTower[10] = new int[]{0,8,8,0,0,0,0,8}; MatTower[11] = new int[]{0,30,0,0,0,0,0,0}; MatTower[12] = new int[]{12,0,0,12,0,0,0,12}; |
|
|
|
|
|
jhv2
|
Re: Mutli-Dimensional arrays
« Reply #4 on: Feb 29th, 2004, 1:18am » |
|
I got it to work, Thanks!
|
|
|
|
|