Arrays

Revision as of 18:10, 30 July 2009 by rosettacode>Guga360 (Created page with '{{task|Basic language learning}} This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. In this task, the goal is to show ba…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array.

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

In this task, the goal is to show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element. (if available, show both fixed-length arrays and dynamic arrays, pushing a value into it.)

C++

<lang cpp>int* myArray = new int[10];

myArray[0] = 1; myArray[1] = 3;

cout << myArray[1] << endl;</lang>

Dynamic Array (STL vector)

<lang cpp>vector<int> myArray2;

myArray2.push_back(1); myArray2.push_back(3);

myArray2[0] = 2;

cout << myArray2[0] << endl;</lang>

C#

<lang csharp>int[] array = new int[10];

array[0] = 1; array[1] = 3;

Console.WriteLine(array[0]);</lang>

Dynamic Array (ArrayList)

<lang csharp> using System; using System.Collections;

ArrayList<int> array = new ArrayList<int>();

array.Add(1); array.Add(3);

array[0] = 2;

Console.WriteLine(array[0]);</lang>

Python

Dynamic Array (List)

<lang python>array = []

array.append(1) array.append(3)

array[0] = 2

print array[0]</lang>

Ruby

Dynamic Array (List)

<lang ruby>arr = Array.new

arr << 1 arr << 3

arr[0] = 2

puts arr[0]</lang>