Wenn Sie versuchen, dieses 4 Zeichen „Mano“ hinzufügen, um eine StrigBuilder Kapazitäts 2, wobei das Verfahren expandCapacity von AbstractStringBuilder mit minimumCapacity genannt werden, da die MinimumSize- die neue Zeichenfolge zuweisen benötigt:
void expandCapacity(int minimumCapacity) {
int newCapacity = value.length * 2 + 2;
if (newCapacity - minimumCapacity < 0)
newCapacity = minimumCapacity;
if (newCapacity < 0) {
if (minimumCapacity < 0) // overflow
throw new OutOfMemoryError();
newCapacity = Integer.MAX_VALUE;
}
value = Arrays.copyOf(value, newCapacity);
}
Die Das erste, was es zu tun versucht, ist, die aktuelle Kapazität zu verdoppeln und die Summe 2. Deine ursprüngliche Kapazität ist 2, also ist die neue Kapazität 6. Dies ist größer als 4 (die Länge von "Mano"), deshalb bist du der Grund siehe 6.