Creating an Array: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 96: Line 96:
# This lets us create the above programmatically:
# This lets us create the above programmatically:
array = [ [*0..4].map {|i| [i] * 6} ]
array = [*0..4].map {|i| [i] * 6}
#=> [[[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4]]]
#=> [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4]]


==[[Ada]]==
==[[Ada]]==

Revision as of 21:51, 21 January 2007

Task
Creating an Array
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 create an array.

mIRC

Interpeter: mIRC Script Editor Libraries: mArray Snippet

alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }

Visual Basic 2005.Net

Dim myArray as new ArrayList
Dim myArray2 as new ArrayList = { "Item1", "Item2" }

Quick Basic

Interpeter: QB 4.5 or PB 7.1 Libraries: None are needed

 ' $DYNAMIC
 DIM SHARED myArray(-10 TO 10, 10 TO 30) AS STRING
 REDIM SHARED myArray(20, 20) AS STRING
 myArray(1,1) = "Item1"
 myArray(1,2) = "Item2"

Javascript

 var myArray = new Array();
 var myArray2 = new Array("Item1","Item2");
 var myArray3 = ["Item1", "Item2"];

3DS Max 8 - MaxScript

 myArray = #()
 myArray2 = #("Item1", "Item2")


Python

Interpeter: Python Libraries: None are needed

Array=[
       [0,0,0,0,0,0],
       [1,1,1,1,1,1],
       [2,2,2,2,2,2],
       [3,3,3,3,3,3]
      ]
#You would call the array by this code. This will call the 3rd 1 on the second list
Array[1][3]}

Alternatively you can create it programmatically. e.g.

Array = [ [i]*6 for i in range(4) ]

Create an empty array:

Array = []

Perl

Interpeter: Perl Libraries: None are needed

@Array=(
        [0,0,0,0,0,0],
        [1,1,1,1,1,1],
        [2,2,2,2,2,2],
        [3,3,3,3,3,3]
      );
#You would call the array by this code. This will call the 3rd 1 on the second list
print $Array[1][3];
 # Alternative:
 my @array_using_qw = qw/coffee sugar cream/;

PHP

$array = array(
               array(0, 0, 0, 0, 0, 0),
               array(1, 1, 1, 1, 1, 1),
               array(2, 2, 2, 2, 2, 2),
               array(3, 3, 3, 3, 3, 3)
         );
#You would call the array by this code. This will call the 3rd 1 on the second list
echo $array[1][3];

Ruby

array = [
  [0, 0, 0, 0, 0, 0],
  [1, 1, 1, 1, 1, 1],
  [2, 2, 2, 2, 2, 2],
  [3, 3, 3, 3, 3, 3]
]

# You would call the array by this code. This will call the 3rd 1 on the second list
array[1][3]
# You can also create a sequential array from a range using the 'splat' operator:
array = [*0..5]

#=> [0,1,2,3,4,5]

# This lets us create the above programmatically:
array = [*0..4].map {|i| [i] * 6}

#=> [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4]]

Ada

Compiler: GCC 4.1.2

type Arr is array (Positive range <>) of Integer;
Uninitialized : Arr (1 .. 10);
Initialized_1 : Arr (1 .. 20) := (others => 1);
Initialized_2 : Arr := (1 .. 30 => 2);
Const         : constant Arr := (1 .. 10 => 1, 11 .. 20 => 2, 21 | 22 => 3);

Template:Array operation

OCaml

let nbItems = 5 and initialValue = 0 in
   make_vect nbItems initialValue


Java

For example for an array of 10 int values:

int[] intArray = new int[10];

ANSI C

Compiler: GCC, MSVC, BCC, Watcom

// Dynamic int n = 10 * sizeof(int); int *myArray = (int*)malloc(n); memset(myArray, 0, n); myArray[0] = 1; myArray[1] = 2;

// Static int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */

ANSI C++

Compiler: GCC, MSVC, BCC, Watcom

 // Dynamic
 const int n = 10;
 int myArray = new int[n];
 myArray[0] = 1;
 myArray[1] = 2;
 delete[] myArray;
 // Static
 int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
 // STL
 std::vector<int> myArray3(10);
 myArray3.push_back(1);
 myArray3.push_back(2);
 // Qt
 QVector<int> myArray4(10);
 myArray4.push_back(1);
 myArray4.push_back(2);
 // MFC
 CArray<int,int> myArray5(10);
 myArray5.Add(1);
 myArray5.Add(2);