I'll try to keep this as much language agnostic as I can but I'll give the examples in C# since I now know how to solve it that language.

The question came to mind when I was thinking of how could I instantiate many objects of the same type.

Lets say I wanted to create one rectangle, I would do:

Rectangle rect1 = new Rectangle()

Now, if I wanted to create 2 I would do.

Rectangle rect1 = new Rectangle()
Rectangle rect2 = new Rectangle()

This of course is not efficient when you need to create many rectangles.

The problem is, I couldn't use loops just like that to create a many rectangles:

for (int i = 0; i < 100; i++){
    Rectangle rect1 = new Rectangle()

This obviously doesn't achieve what I am after since I would just create 100 rect1's.

I was pointed out that you could do something like this: Thanks mootinator

var rectangles = new Rectangle[100];
for (int i = 0; i < 100; i++){
  rectangles[i] = new Rectangle();

For what I understood, the code creates a data structure (Array? ArrayList?) of they type Rectangle inside you actually "store" Rectangles.

The hardest thing for me was putting a question for this.

So the questions are:

  1. What is this called?
  2. Is this standard / "common" in most OOP based languages? The question actually came up in ActionsScript 3, but is is useful /necessary in any language it might seem.
  3. Is this the best way for doing this considering that I would the like to, for example, loop this Array and apply a method .Move().
  4. Are there? and what are the other options to do this.
  • 3
    This is an array. They are used frequently along with lists.
    – user1249
    Commented Sep 19, 2011 at 22:47
  • @ThorbjørnRavnAndersen Thanks! just in case, what confused me specifically is that it is an array of a specific type.
    – Trufa
    Commented Sep 19, 2011 at 22:50
  • The specific point then is that it's a typed array. ActionScript has a rather weak type system; many (most?) popular languages other than scripting languages have stronger typing. Commented Sep 20, 2011 at 7:18

2 Answers 2


You are creating an array, which is something built in to the language, in the case of C#. If you want a nice collection object you can create the collection you want, say an ArrayList, or better, ArrayList< Rectangle > and call the Add() method in a loop, adding a New Rectangle each time.

The specifics do vary from language to language, but nothing makes you write rectangle1,rectangle2,...rectangle100000, since that wouldn't be practical. In general you would call "a bunch of objects that don't each need to have a unique name" a collection. Array is sometimes a special built-in language concept, while other collections are often implemented on top of them, but sometimes other kinds of collections (dictionary, list) are the built-in basis for other collections.


It's an Array of objects. Other implementations might be a linked list or a tree that mimics an array but allows for expansion/deletion: The infamous ArrayList. I'd recommend a book on Data Structures as the more you know, the better off you are. As to what you store in the Data Structure itself? It doesn't matter, objects are a code construct in that respect. Assuming first-class objects, you can then pass them to and fro to any data structure.

Well sort of. Technically you're passing a message to that data structure to create a new object inside itself that corresponds to the object "passed" as an argument. To use the car example that I always do, the message tells the data structure to "store this car" and the data structure builds a car and stores it.

  • Thank you for the answer, it seems to be something simple that I thought it might be. Thanks for the advice, I don't think I'll dive into a book right now, but eventually. I will definitely read up on data structures since I'm clearly missing some technical an theoretic background. Thanks again!
    – Trufa
    Commented Sep 19, 2011 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.