SLIDE 1 / 5

1. Two Levels of Memory in the Heap

In Java, an object array does NOT store objects inside its cells. The array is a container object in the Heap whose slots hold only POINTERS to independent instances in memory.

Stack plantel @0x1000 Heap: Array @0x1000 new Persona[3] [0] @0x5A00 → puntero [1] @0x5B00 → puntero [2] @0x5C00 → puntero Heap: Instancias Reales Persona @0x5A00 "Messi", Camiseta 10 Persona @0x5B00 "Di María", Camiseta 11 Persona @0x5C00 "Dibu", Camiseta 23
Slot Inspector
Stack Access: plantel -> Heap @0x1000
Array Cell: plantel[0] = @0x5A00
Target Heap Object: Persona("Messi", 10)
Persona[] plantel = new Persona[3];
plantel[0] = new Persona("Messi", 10);
plantel[1] = new Persona("Di María", 11);
plantel[2] = new Persona("Dibu", 23);

2. Two-Step Creation and the null Trap

new Persona[3] does NOT instantiate 3 persons. It allocates an array with 3 empty slots set to null. Calling a method on a slot before populating it throws NullPointerException.

Paso 1: Contenedor Únicamente Persona[] lista = new Persona[3]; [0] null ✕ [1] null [2] null ⚡ NullPointerException lista[0].getNombre(); ¡CRASH EN RUNTIME! lista[0] no tiene ningún objeto
Initialization Simulator
State at Slot [0]: null (Empty)
Invocation Result: NullPointerException
// PASO 1: Contenedor (3 casillas en null)
Persona[] lista = new Persona[3];

// PASO 2: Instanciar cada elemento
lista[0] = new Persona("Lautaro", 22);
System.out.println(lista[0].getNombre()); // OK!

3. Safe Traversal: The Null Guard Filter

Real-world arrays are seldom 100% full. Any for or for-each loop must guard calls with `if (elem != null)` to safely process only instantiated entries and skip blank slots.

[0] "Ana" [1] "Leo" [2] null [3] "Dibu" [4] null i = 0 if (p != null) -> true ✓ p.presentarse() ejecutado correctamente
Loop Stepper
Current index: i = 0
Value at plantel[i]: Persona("Ana")
Guard Evaluation: true (Pasa)
for (Persona p : plantel) {
  if (p != null) { // <-- Blindaje obligatorio
    p.presentarse();
  }
}

4. Physical Capacity vs Logical Size

An allocated array of size 6 may hold only 3 active items. A `size` integer tracks the next free slot. When full (`size == capacity`), it grows by copying into a larger array with Arrays.copyOf().

Memoria del Array (Capacidad = 6) Activos: 3/6 [0] Ana [1] Leo [2] Dibu [3] null [4] null [5] null Insertar en posición libre: plantel[cantidad] plantel[3] = nuevo; cantidad++; // cantidad pasa a 4 No se recorre todo el arreglo; la inserción es O(1).
Dynamic List Operations
Physical Capacity: plantel.length = 6
Logical Count: cantidad = 3
Free Slots: 3 casillas libres
public void agregar(Persona p) {
  if (cantidad == plantel.length) {
    plantel = Arrays.copyOf(plantel, plantel.length * 2);
  }
  plantel[cantidad] = p;
  cantidad++;
}

5. Sorting Object Arrays: Writing the Criterion by Hand

Arrays.sort() can sort a primitive array on its own. With objects it does not know what to compare: you write the comparison loop by hand, field by field. Sorting swaps pointers in the array without cloning objects in the Heap — you will automate this criterion in lesson 16.

Array plantel: Intercambio de Referencias [0] "Di María" Camiseta: #11 [1] "Dibu" Camiseta: #23 [2] "Messi" Camiseta: #10 Estado Inicial: Desordenado por camiseta [11, 23, 10] // Arrays.sort(plantel) revienta: ClassCastException Los objetos en el Heap nunca se mueven ni se copian; solo cambian los punteros en las casillas.
Sort Strategies
Active Criterion: Original insertion order
Active Comparison: plantel
// Ascendente: buscás el menor y lo llevás al frente
if (plantel[j].getCamiseta() < plantel[menor].getCamiseta()) {
  menor = j;
}

// Descendente: mismo bucle, comparación invertida
if (plantel[j].getCamiseta() > plantel[mayor].getCamiseta()) {
  mayor = j;
}