Arreglos estáticos
Un arreglo estático (también llamado array de tamaño fijo) es una estructura de datos lineal que almacena una colección de elementos del mismo tipo en posiciones de memoria contiguas. Su tamaño se define en el momento de la creación y no puede cambiar durante la ejecución del programa.
¿Cómo se implementan en memoria?
El arreglo estático se almacena como un bloque continua de memoria, donde:
- El primer elemento se guarda en una dirección base.
- Los demás elementos se colocan en posiciones consecutivas, calculadas por desplazamiento.
Fórmula para acceder a un elemento
Dirección del elemento = Dirección base + índice * tamaño del tipo de dato
Según Cormen et al. (2022), esta propiedad de acceso por índice permite que las operaciones de lectura y escritura sean de tiempo constante O(1).
Ventajas
- Acceso directo y rápido a cualquier elemento (
O(1)
). - Eficiencia en el uso de CPU y caché por memoria contigua.
- Sencillez de implementación.
Limitaciones
- Tamaño fijo: se debe conocer de antemano
- Ineficacia al insertar o eliminar (requiere mover elementos)
- Uso potencialmente ineficiente de memoria (espacios sin utilizar)
Aplicaciones prácticas
Área | Uso del arreglo estático |
---|---|
Imágenes digitales | Representación de píxeles |
Juegos 2D | Matrices para representar mapas/tableros |
Sensores IoT | Buffer de muestras de tamaño fijo |
Simulaciones científicas | Modelo de matrices o vectores o vectores físicos |
Representación visual
Tenemos un arreglo estático de 4 posiciones. El arreglo no puede crecer, solo cambiar sus valores.
Ejemplos
- Paradigma: Orientado a Objetos
- Paradigma: Procedural
- Paradigma: Funcional
- Código Java Ejemplo
- Test Unitario
StaticArray.java
package datastructures;
public class StaticArray {
private final int[] data;
public StaticArray(int size) {
data = new int[size];
}
public void set(int index, int value) {
if (index < 0 || index >= data.length)
throw new IndexOutOfBoundsException("Índice fuera de rango");
data[index] = value;
}
public int get(int index) {
if (index < 0 || index >= data.length)
throw new IndexOutOfBoundsException("Índice fuera de rango");
return data[index];
}
public int length() {
return data.length;
}
}
StaticArrayTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StaticArrayTest {
@Test
void testSetAndGet() {
StaticArray array = new StaticArray(3);
array.set(0, 5);
array.set(2, 10);
assertEquals(5, array.get(0));
assertEquals(10, array.get(2));
}
@Test
void testInvalidIndex() {
StaticArray array = new StaticArray(2);
assertThrows(IndexOutOfBoundsException.class, () -> array.set(3, 99));
}
}
- Código Python Ejemplo
- Test Unitario
static_array.py
def create_static_array(size: int) -> list[int]:
return [0] * size;
def set_value(arr: list[int], index: int, value: int) -> None:
if index < 0 or index >= len(arr):
raise IndexError("Índice fuera de rango")
arr[index] = value
def get_value(arr: list[int], index: int) -> int:
if index < 0 or index >= len(arr):
raise IndexError("Índice fuera de rango")
return arr[index]
test_static_array.py
import unittest
from static_array import create_static_array, set_value, get_value
class TestStaticArray(unittest.TestCase):
def test_access(self):
arr = create_static_array(4)
set_value(arr, 0, 12)
set_value(arr, 3, 8)
self.assertEqual(get_value(arr, 0), 12)
self.assertEqual(get_value(arr, 3), 8)
def test_out_of_bounds(self):
arr = create_static_array(2)
with self.assertRaises(IndexError):
set_value(arr, 5, 1)
if __name__ == '__main__':
unittest.main()
- Código TypeScript Ejemplo
- Test Unitario
structures.ts
export const createArray = (size: number): number[] => Array.from({ length: size }, () => 0);
export const setValue = (arr: number[], index: number, value: number): number[] => {
if (index < 0 || index >= arr.length) throw new Error("Índice fuera de rango");
const copy = [...arr];
copy[index] = value;
return copy;
};
export const getValue = (arr: number[], index: number): number => {
if (index < 0 || index >= arr.length) throw new Error("Índice fuera de rango");
return arr[index];
};
structures.test.ts
import { createArray, setValue, getValue } from "./staticArray";
test("crear y acceder arreglo", () => {
let arr = createArray(3);
arr = setValue(arr, 1, 7);
expect(getValue(arr, 1)).toBe(7);
});
test("índice fuera de rango", () => {
const arr = createArray(2);
expect(() => setValue(arr, 5, 99)).toThrow("Índice fuera de rango");
});
Referencias
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2022). Introduction to Algorithms (4.ª ed.). MIT Press.
- Goodrich, M. T., Tamassia, R., & Goldwasser, M. H. (2014). Data Structures and Algorithms in Python. Wiley.
- Oracle Java Documentation. (2024).
- TypeScript Handbook. (2024).
- Python Docs. (2024).