Multi-dimensional arrays can be represented by arrays of arrays. For example, suppose that you want to create a 3x3 matrix, called Brady, and fill it with the strings shown in the following table:
col 0 col 1 col 2
row 0 "Marsha" "Carol" "Greg"
row 1 "Jan" "Alice" "Peter"
row 2 "Cindy" "Mike" "Bobby"
To create the array, you should first allocate storage for each element:
Brady = new Array(3) for (i = 0; i < Brady.length; ++ i)
Brady [i] = new Array(3);
Although Brady is just a one-dimensional array, the fact that each of Brady's elements is initialized with yet another one-dimensional array means that Brady is an array of arrays--in other words, a two-dimensional array. Each element in the Brady array is a row in the matrix, and each row is an array of three elements.
It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com. Already a member?
To become a member of DevX.com create your Member Profile by completing the form below. Membership is free!
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.