Anonymous user
Multiple distinct objects: Difference between revisions
markup fixes: <code> to <lang>, <tt> to <code>
(added perl) |
(markup fixes: <code> to <lang>, <tt> to <code>) |
||
Line 9:
=={{header|Ada}}==
<lang ada>
A : array (1..N) of T;
</
Here N can be unknown until run-time. T is any constrained type.
=={{header|C}}==
<
foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
init_foo(&foos[i]);
</
(Or if no particular initialization is needed, skip that part, or use <tt>calloc</tt>.)
Line 25:
=={{header|C++}}==
Using only language primitives:
<
// this assumes T is a default-constructible type (all built-in types are)
T* p = new T[n]; // if T is POD, the objects are uninitialized, otherwise they are default-initialized
Line 31:
// when you don't need the objects any more, get rid of them
delete[] p;
</
Using the standard library
<
#include <vector>
Line 48:
for (int i = 0; i < n; ++i)
vec3.push_back(makeDistinctObject());
</
Instead of <
For polymorphic objects:
<
#include <vector>
Line 89:
// set the pointers to NULL after deleting; again, using a smart pointer
// would remove this need)
</
Of course, also in this case one can use the other sequence containers or plain new/delete instead of <tt>vector</tt>.
=={{header|Common Lisp}}==
The mistake is often written as one of these:
<
(make-list n :initial-element (make-the-distinct-thing))
(make-array n :initial-element (make-the-distinct-thing))
</
which are incorrect since <
<
(loop repeat n collect (make-the-distinct-thing))
</
which evaluates <
=={{header|Haskell}}==
Below, we are assuming that <tt>makeTheDistinctThing</tt> is a monadic expression (i.e. it has type <
<
replicateM n makeTheDistinctThing
</
in an appropriate do block. If it is distinguished by, say, a numeric label, one could write
<
mapM makeTheDistinctThing [1..n]
</
An incorrect version:
<
do x <- makeTheDistinctThing
return (replicate n x)
</
=={{header|Java}}==
{{works with|Java|1.5+}}
simple array:
<
Foo[] foos = new Foo[n]; // all elements initialized to null
for (int i = 0; i < foos.length; i++)
Line 132:
Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo()); // new Foo() only evaluated once
</
simple list:
<
List<Foo> foos = new ArrayList<Foo>();
for (int i = 0; i < n; i++)
Line 142:
// incorrect:
List<Foo> foos_WRONG = Collections.nCopies(n, new Foo()); // new Foo() only evaluated once
</
Generic version for class given at runtime:
It's not pretty but it gets the job done. The first method here is the one that does the work. The second method is a convenience method so that you can pass in a <tt>String</tt> of the class name. When using the second method, be sure to use the full class name (ex: "java.lang.String" for "String"). <tt>InstantiationException</tt>s will be thrown when instantiating classes that you would not normally be able to call <tt>new</tt> on (abstract classes, interfaces, etc.).
<
public static <E> List<E> getNNewObjects(int n, Class<? extends E> c){
List<E> ans = new LinkedList<E>();
Line 165:
return getNNewObjects(n, Class.forName(className));
}
</
=={{header|OCaml}}==
Line 171:
Incorrect:
<
Array.make n (new foo);; (* here (new foo) can be any expression that returns a new object, record, array, or string *)
</
which is incorrect since <
<
Array.init n (fun _ -> new foo);;
</
=={{header|Perl}}==
incorrect:
<
(Foo->new) x $n # here Foo->new can be any expression that returns a reference representing a new object
</
which is incorrect since <
A correct version is:
<
map { Foo->new } 1 .. $n;
</
which evaluates <tt>Foo->new</tt> <var>$n</var> times and collects each result in a list.
=={{header|Python}}==
The mistake is often written as:
<
[Foo()] * n # here Foo() can be any expression that returns a new object
</
which is incorrect since <tt>Foo()</tt> is only evaluated once. A common correct version is:
<
[Foo() for i in xrange(n)]
</
which evaluates <tt>Foo()</tt> <var>n</var> times and collects each result in a list. This last form is also discussed [[Two-dimensional array (runtime)#Python|here]], on the correct construction of a two dimensional array.
=={{header|Ruby}}==
The mistake is often written as one of these:
<
[Foo.new] * n # here Foo.new can be any expression that returns a new object
Array.new(n, Foo.new)
</
which are incorrect since <
<
Array.new(n) { Foo.new }
</
which evaluates <
|