What is this structure called?

What it is a data structure of a certain type called?

  • 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: http://programmers.stackexchange.com/users/6847/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: What is this called? 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. 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(). Are there? and what are the other options to do this.

  • Answer:

    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.

Trufa at Programmers Visit the source

Was this solution helpful to you?

Other answers

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.

World Engineer

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.