Collections: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 22: Line 22:


In Python practically everything is an object, so using any of the provided structures can function as a collection.
In Python practically everything is an object, so using any of the provided structures can function as a collection.
<pre>

collection = [0, '1'] # Lists by convention used for heterogenous objects
collection = [0, '1'] # Lists by convention used for heterogenous objects
collection = (0, 1) # Tuples by convention used for homogenous objects

collection = {0: "zero", 1: "one"} # Dictionaries (a.k.a Hash)
or
collection = set(0, '1')

</pre>
collection = (0, 1) # Tuples by convention used for homogenous objects

or

collection = {0: "zero", 1: "one"} # Dictionaries (a.k.a Hash)


==[[Ruby]]==
==[[Ruby]]==

Revision as of 14:42, 25 January 2007

Task
Collections
You are encouraged to solve this task according to the task description, using any language you may know.

Collections are used to store objects and not primitive types.


Java

  ArrayList arrayList = new ArrayList();
  arrayList.add(new Integer(0));

PHP

  $students = array();
  array_push($students, array('name' => 'Joe Smith', 'age' => 21, height=> '72.5', gpa => 3.42 ));

Python

Interpreter: Python 2.5

In Python practically everything is an object, so using any of the provided structures can function as a collection.

collection = [0, '1'] # Lists by convention used for heterogenous objects
collection = (0, 1) # Tuples by convention used for homogenous objects
collection = {0: "zero", 1: "one"} # Dictionaries (a.k.a Hash)
collection = set(0, '1')

Ruby

Ruby is a 100% object oriented language, so you can use the default Array or Hash structures as collection objects.