The dimension of an array is dependent on the number of indexes it has. An array can have 1 or more indexes.
The most common are 1D, 2D and 3D arrays (1 index, 2 indexes, or 3 indexes)
1D Arrays
A 1D Array is like a list of items. The code below creates an array with 5 slots in a 1D array.
Dim myArray$(5)
myArray$(1) = "List Item 1"
myArray$(2) = "List Item 2"
myArray$(3) = "List Item 3"
myArray$(4) = "List Item 4"
myArray$(5) = "List Item 5"
The data is stored like this :
| Array Index | Data |
|---|---|
| 1 | List Item 1 |
| 2 | List Item 2 |
| 3 | List Item 3 |
| 4 | List Item 4 |
| 5 | List Item 5 |
Uses for 1D Arrays
A 1D Array can be used to hold any kind of group or set of data that is arranged well in a list :
- Hold the game objects in your game, as types.
- A list of high scores (if only the scores are stored), or the scores are held in types.
- A list of names or name parts to be chosen randomly.
- A simple game character inventory.
- Data for a deck of cards.
2D Arrays
A 2D array has 2 array indexes and stores data in a 2D table or 2D grid. The code below creates a 2D array.
Dim myArray$(2,2)
myArray$(1,1) = "1,1"
myArray$(1,2) = "1,2"
myArray$(2,1) = "2,1"
myArray$(2,2) = "2,2"
When created, the data will be stored like this :
| 1 | 2 | |
|---|---|---|
| 1 | 1,1 | 1,2 |
| 2 | 2,1 | 2,2 |
Uses for 2D Arrays
- Storing level data for 2D games.
3D Arrays
A 3D Array stores data in a 3D grid.
Uses for 3D Arrays
Array Dimensions in Other Languages
Multi-dimensional array support can vary from language to language. The general idea is the same, however, some languages may have limitations or do something slightly differently than PlayBasic. This section deals with those differences.
Basic4GL
Basic4GL places each array index inside of it's own set of parenthesis.
Dim myArray$(2)(2)
myArray$(1)(1) = "1,1"
'and so on
Related Pages
| Categories: Arrays |