Algorithmic Solutions > LEDA > LEDA Guide> Simple, Basic, and Advanced Data Types > Basic Data Types > Two Dimensional Arrays

## Two Dimensional Arrays

The data type array2 can be used to store a number of objects of an arbitrary type T. The objects can be accessed uniquely by two indices from two integer intervals I and J.

This data type is closely related to One Dimensional Arrays.

### Example

The following program shows how to use Two Dimensional Arrays. It generates an `array A` of 10 times 20 ints with indices from 1 to 10 and 31 to 50. Then it assigns value `i+j` to `A(i,j)`. Finally, it shows how elements can be accessed by their indices.
```#include <LEDA/core/array2.h>

int main()
{
leda::array2<int> A(1,20,31,50);
int i;
for (i=1; i<=10; i++) {
int j;
for (j=31;j<=50; j++) {
A(i,j)=i+j;
}
}
std::cout << A(3,43) << " " << A(10,31) << std::endl;

return 0;
}```

### Strengths

• access and insertion of objects by indices in constant time
• induced order on objects
• index sets can be arbitrary intervals of integers (do not have to start at 0)
• array bounds are checked

• waste of space if array is bigger than the currentl number of objects (alternatives: Linear Lists, Sets)
• searching for an element is slow (proportional to the size of the array) (alternative: Sets)
• no simple iteration over the current elements (alternatives: Linear Lists, Sets)
• initialization is necessary

### Tips

• Use Two Dimensional Arrays if you know the number of elements to store (at least approximately) beforehand and you need to access the elements efficiently by two integer indices.
• Otherwise consider using one of the combinations list of lists, set of sets, list of sets, set of lists, of Linear Lists or Sets.
• For matrix operations consider using Double Valued Matrices, Matrices with Integer Entries.

One Dimensional Arrays

Linear Lists

Sets

Double Valued Matrices

Matrices with Integer Entries

Two-Dimensional Node Array

Manual Entries:

Manual Page Two Dimensional Arrays