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.
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 (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.
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().
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.
// 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;
}