Arrays: Difference between revisions

From Rosetta Code
Content added Content deleted
(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…')
 
No edit summary
Line 4: Line 4:


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.)
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.)

Please discusss at Village Pump: {{vp|Arrays}}.


=={{header|C++}}==
=={{header|C++}}==

Revision as of 18:14, 30 July 2009

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

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

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.)

Please discusss at Village Pump: Arrays.

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>