Logo

Programming-Idioms

Preallocate memory in the list x for a minimum total capacity of 200 elements.

This is not possible in all languages. It is only meant as a performance optimization, should not change the length of x, and should not have any effect on correctness.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
with Ada.Containers.Vectors;
declare
   X : Vector;
begin
   Reserve_Capacity (X, Capacity => 200);
end;
if cap(x) < 200 {
	y := make([]T, len(x), 200)
	copy(y, x)
	x = y
}
import "slices"
x = slices.Grow(x, 200)
import java.util.ArrayList;
import java.util.List;
List<T> x = new ArrayList<>(200);
import java.util.ArrayList;
ArrayList<?> x = new ArrayList<>();
x.ensureCapacity(200);
Uses Classes;
x.capacity := 200;
SetLength(a, 200);
my @list = (undef) x 200;
x.reserve(200);
x := Array new: 200.
"#(nil nil nil nil nil ...)"