> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/ud2-programacion-estructurada/arrays/vectores-n-dimensionales.md).

# Vectores N dimensionales

Ata o momento vimos vectores son **unidimensionais**: só teñen unha lonxitude

<figure><img src="/files/wnCmrusysKMOavkAKtCv" alt=""><figcaption></figcaption></figure>

Pero pode acontecer que os nosos datos se refiran a entidades que se caracterizan por máis dunha propiedade. Neste caso non só basta co seu índice para describilos.

#### 11.5.1. Matrices<i class="fa-link">:link:</i> <a href="#id-1151-matrices" id="id-1151-matrices"></a>

Un vector que posúa lonxitude e anchura coñécese como **matriz**. Para identificar **cada elemento** da matriz necesitamos **dous índices**

<figure><img src="/files/Djmk8HRCD8f7m6ZMUvwk" alt=""><figcaption></figcaption></figure>

A declaración dunha matriz faise do seguinte xeito:

```java
tipo[][] nomeMatriz;
```

Por exemplo podemos crear unha matriz como a do exemplo 5x4:

```java
int[][] datos = new int[5][4];
```

Para recorrer as matrices utilízanse bucles aniñados:

```java
for (int x = 0; x < matriz.length; x++){
    //Neste momento estamos recorrendo o eixo x, necesitamos agora recorrer o eixe y
    for (int y = 0; y < matriz[x].length; y++){
        System.out.println(matriz[x][y]);
    }
}
```

#### 11.5.2. Interpretacións das matrices<i class="fa-link">:link:</i> <a href="#id-1152-interpretacions-das-matrices" id="id-1152-interpretacions-das-matrices"></a>

Podemos interpretar as matrices de diferentes xeitos. No caso anterior que puxemos como exemplo utilizamos como un eixe de coordenadas. Pero tamén o podemos interpretar como unha cuadrícula de filas e columnas

<figure><img src="/files/eMApJRXiMXq1HmoC4ZCe" alt=""><figcaption></figcaption></figure>

Ademais a hora de codificar esta estrutura temos dous xeitos: considerando as columnas como o primeiro índice, ou considerando as filas como o primeiro índice:

```java
// Columnas como primeiro índice
int[][] columnas_filas = new int[5][4];

// Filas como primeiro índice
int[][] filas_columnas = new int[4][5];
```

#### 11.5.3. Vectores tridimensionais<i class="fa-link">:link:</i> <a href="#id-1153-vectores-tridimensionais" id="id-1153-vectores-tridimensionais"></a>

Podemos engadir unha nova dimensión para crear matrices con anchura, altura e profundidade. Neste caso temos 3 índices

<figure><img src="/files/JFkRxn74XzRY43wiol91" alt=""><figcaption></figcaption></figure>

```java
double[][][] datos = new datos[4][6][2];
```

Aquí un exemplo de como recorrelo:

```java
// Percorrer o array tridimensional
for (int i = 0; i < datos.length; i++) { // Primeiro nivel
    for (int j = 0; j < datos[i].length; j++) { // Segundo nivel
        for (int k = 0; k < datos[i][j].length; k++) { // Terceiro nivel
            System.out.println("Elemento en [" + i + "][" + j + "][" + k + "] = " + datos[i][j][k]);
        }
    }
}
```

É posible realizar matrices de 4 dimensións, 5,etc.. de xeito similar a como se utilizan con 2 e 3 dimensións.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/ud2-programacion-estructurada/arrays/vectores-n-dimensionales.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
