Creating an Array: Difference between revisions

Properly ordering the languages
(Properly ordering the languages)
Line 6:
In this task, the goal is to create an [[array]].
 
==[[mIRCAda]]==
'''InterpeterCompiler:''' mIRCGCC Script Editor4.1.2
type Arr is array (Positive range <>) of Integer;
'''Libraries:''' [[mArray Snippet]]
Uninitialized : Arr (1 .. 10);
alias creatmearray { .echo -a $array_create(MyArray, 5, 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);
 
{{array operation}}
==[[Visual Basic .Net]]==
'''Compiler:''' [[Visual Studio .NET]] 2005
Dim myArray() as String = New String() {"Hello", "World", "!"}
 
==[[BASIC]]==
Line 24 ⟶ 25:
myArray(1,2) = "Item2"
 
==[[JavaScriptC]]==
'''Compiler:''' GCC, MSVC, BCC, Watcom
var myArray = new Array();
var myArray2 = new Array("Item1","Item2");
var myArray3 = ["Item1", "Item2"];
 
'''Libraries:''' [[None are needed]]
==[[3DS Max 8 - MaxScript]]==
// Dynamic
myArray = #()
int n = 10 * sizeof(int);
myArray2 = #("Item1", "Item2")
int *myArray = (int*)malloc(n);
if(myArray != NULL)
{
memset(myArray, 0, n);
myArray[0] = 1;
myArray[1] = 2;
free(myArray);
myArray = NULL;
}
 
 
==[[Python]]==
'''Interpeter:''' Python 2.3, 2.4, 2.5
'''Libraries:''' [[None are needed]]
// Static
Array=[
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
[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]
 
==[[C plus plus|C++]]==
Alternatively you can create it programmatically with a list comprehension:
'''Compiler:''' [[GCC]], [[Visual C plus plus|Visual C++]], [[BCC]], [[Watcom]]
 
Array = [ [i]*6 for i in range(4) ]
 
// Dynamic
Create an empty array:
const int n = 10;
int* myArray = new int[n];
if(myArray != NULL)
{
myArray[0] = 1;
myArray[1] = 2;
delete[] myArray;
myArray = NULL;
}
 
// Static
Array = []
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
 
'''Libraries:''' [[STL]]
// STL
std::vector<int> myArray3(10);
myArray3.push_back(1);
myArray3.push_back(2);
 
'''Libraries:''' [[Qt]]
// Qt
QVector<int> myArray4(10);
myArray4.push_back(1);
myArray4.push_back(2);
 
'''Libraries:''' [[Microsoft Foundation Classes]]
// MFC
CArray<int,int> myArray5(10);
myArray5.Add(1);
myArray5.Add(2);
 
==[[C#]]==
 
Example of array of 10 int types:
 
int[] numbers = new int[10];
 
Example of array of 3 string types:
 
string[] words = { "these", "are", "arrays" };
 
You can also declare the size of the array and initialize the values at the same time:
 
int[] more_numbers = new int[3]{ 21, 14 ,63 };
 
 
For Multi-Deminsional arrays you delcare them the same except for a comma in the type declaration.
 
The following creates a 3x2 int matrix
int[,] number_matrix = new int[3][2];
 
As with the previous examples you can also initialize the values of the array, the only difference being each row in the matrix must be enclosed in its own braces.
 
string[,] string_matrix = { {"I","swam"}, {"in","the"}, {"freezing","water"} };
 
or
 
string[,] funny_matrix = new string[2][2]{ {"clowns", "are"} , {"not", "funny"} };
 
==[[Perl]]==
Line 98 ⟶ 153:
#You would call the array by this code. This will call the 3rd 1 on the second list
echo $array[1][3];
 
==[[Python]]==
'''Interpeter:''' Python 2.3, 2.4, 2.5
'''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 with a list comprehension:
 
Array = [ [i]*6 for i in range(4) ]
 
Create an empty array:
 
Array = []
 
==[[Ruby]]==
Line 128 ⟶ 203:
#=> [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]]
 
==[[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);
 
{{array operation}}
 
==[[OCaml]]==
 
Using an array literal:
 
let array = [| 1; 2; 3; 4; 5 |];;
 
To create an array of five elements with the value 0:
 
let num_items = 5 and initial_value = 0;;
let array = Array.make num_items initial_value
 
To create an array with contents defined by passing each index to a callback (in this example, the array is set to the squares of the numbers 0 through 4):
 
let callback index = index * index;;
let array = Array.init 5 callback
 
==[[Java]]==
Line 165 ⟶ 214:
String[] s = {"hello" , "World" };
 
==[[CJavaScript]]==
var myArray = new Array();
'''Compiler:''' GCC, MSVC, BCC, Watcom
var myArray2 = new Array("Item1","Item2");
var myArray3 = ["Item1", "Item2"];
 
==[[3DS Max 8 - MaxScript]]==
'''Libraries:''' [[None are needed]]
myArray = #()
// Dynamic
myArray2 = #("Item1", "Item2")
int n = 10 * sizeof(int);
int *myArray = (int*)malloc(n);
if(myArray != NULL)
{
memset(myArray, 0, n);
myArray[0] = 1;
myArray[1] = 2;
free(myArray);
myArray = NULL;
}
 
==[[mIRC]]==
'''Libraries:''' [[None are needed]]
'''Interpeter:''' mIRC Script Editor
// Static
'''Libraries:''' [[mArray Snippet]]
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }
 
==[[C plus plus|C++OCaml]]==
'''Compiler:''' [[GCC]], [[Visual C plus plus|Visual C++]], [[BCC]], [[Watcom]]
 
Using an array literal:
 
let array = [| 1; 2; 3; 4; 5 |];;
// Dynamic
const int n = 10;
int* myArray = new int[n];
if(myArray != NULL)
{
myArray[0] = 1;
myArray[1] = 2;
delete[] myArray;
myArray = NULL;
}
 
To create an array of five elements with the value 0:
// Static
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
 
let num_items = 5 and initial_value = 0;;
'''Libraries:''' [[STL]]
let array = Array.make num_items initial_value
// STL
std::vector<int> myArray3(10);
myArray3.push_back(1);
myArray3.push_back(2);
 
To create an array with contents defined by passing each index to a callback (in this example, the array is set to the squares of the numbers 0 through 4):
'''Libraries:''' [[Qt]]
// Qt
QVector<int> myArray4(10);
myArray4.push_back(1);
myArray4.push_back(2);
 
let callback index = index * index;;
'''Libraries:''' [[Microsoft Foundation Classes]]
let array = Array.init 5 callback
// MFC
CArray<int,int> myArray5(10);
myArray5.Add(1);
myArray5.Add(2);
 
==[[C#]]==
 
Example of array of 10 int types:
 
int[] numbers = new int[10];
 
Example of array of 3 string types:
 
string[] words = { "these", "are", "arrays" };
 
You can also declare the size of the array and initialize the values at the same time:
 
int[] more_numbers = new int[3]{ 21, 14 ,63 };
 
 
For Multi-Deminsional arrays you delcare them the same except for a comma in the type declaration.
 
The following creates a 3x2 int matrix
int[,] number_matrix = new int[3][2];
 
As with the previous examples you can also initialize the values of the array, the only difference being each row in the matrix must be enclosed in its own braces.
 
string[,] string_matrix = { {"I","swam"}, {"in","the"}, {"freezing","water"} };
 
or
 
string[,] funny_matrix = new string[2][2]{ {"clowns", "are"} , {"not", "funny"} };
 
==[[Smalltalk]]==
Line 258 ⟶ 253:
"Replace apple with orange"
array at: 2 put: 'orange'.
 
==[[Visual Basic .Net]]==
'''Compiler:''' [[Visual Studio .NET]] 2005
Dim myArray() as String = New String() {"Hello", "World", "!"}
79

edits