Creating an Array: Difference between revisions

tag obsolete (Arrays) and remove {header}s
(tag obsolete (Arrays) and remove {header}s)
Line 1:
{{task|Basic language learning}}
 
'''This task is obsolete. Please do not add new code, and merge existing code to the [[Arrays]] task.'''
 
This task is about numerically-indexed arrays. For '''hashes''' or '''associative arrays''', please see [[Creating an Associative Array]].
 
Line 5 ⟶ 8:
In addition, demonstrate how to initialize an array variable with data.
 
=={{header|[[ActionScript}}]]==
<lang actionscript>
// ActionScript arrays are zero-based
Line 19 ⟶ 22:
</lang>
 
=={{header|[[Ada}}]]==
'''Compiler:''' [[GCC]] 4.1.2
 
Line 37 ⟶ 40:
</lang>
 
=={{header|[[ALGOL 68}}]]==
As with all [[ALGOL 68]] declarations the programmer has the choice of using the full declaration syntax, or using [[syntactic sugar]] - c.f. "sugared" variables below.
 
Example of array of 10 integer types:
Line 118 ⟶ 121:
</lang>
 
=={{header|[[AmigaE}}]]==
<lang amigae>DEF ai[100] : ARRAY OF CHAR, -> static
da: PTR TO CHAR,
Line 137 ⟶ 140:
ENDPROC</lang>
 
=={{header|[[AppleScript}}]]==
AppleScript arrays are called lists:
<lang applescript> set empty to {}
Line 145 ⟶ 148:
<lang applescript> set any to {1, "foo", 2.57, missing value, ints}</lang>
 
=={{header|[[AutoHotkey}}]]==
AutoHotkey does not have arrays yet.
However, variables can be set to arbitrary size and pointer operations can be used, simulating arrays. Just without the syntactic sugar of '[]'.
Line 154 ⟶ 157:
</lang>
 
=={{header|[[AWK}}]]==
"Numeric" arrays coincide with hashes in awk. Indices begin at 1. The <tt>split</tt> function can be used to initialize an array from a string with elements separated by a field separator (space by default)
<lang awk>BEGIN {
Line 164 ⟶ 167:
capital["Italy"]="Rome"</lang>
 
=={{header|[[BASIC}}]]==
{{works with|QuickBasic|4.5}}
 
Line 201 ⟶ 204:
<lang freebasic> Dim myArray(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}} </lang>
 
=={{header|[[C}}]]==
Dynamic
<lang c> #include <stdlib.h> /* for malloc */
Line 220 ⟶ 223:
<lang c> int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */</lang>
 
=={{header|[[C++}}]]==
Using dynamically-allocated (i.e. [[Heap]]) memory:
<lang cpp> const int n = 10;
Line 260 ⟶ 263:
myArray5.Add(2);</lang>
 
=={{header|[[C sharp|C#}}]]==
Example of array of 10 int types:
 
Line 287 ⟶ 290:
<lang csharp> string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };</lang>
 
=={{header|[[Clean}}]]==
Array denotations are overloaded in Clean, therefore we explicitly specify the types. There are lazy, strict, and unboxed array.
===Lazy array===
Line 308 ⟶ 311:
array = {x \\ x <- ['a' .. 'z']}</lang>
 
=={{header|[[ColdFusion}}]]==
Creates a one-dimensional Array
<lang coldfusion><cfset arr1 = ArrayNew(1)></lang>
Line 317 ⟶ 320:
''ColdFusion Arrays are '''NOT''' zero-based, they begin at index '''1'''''
 
=={{header|[[Common Lisp}}]]==
Creates a one-dimensional array of length 10. The initial contents are undefined.
<lang lisp>(make-array 10)</lang>
Line 328 ⟶ 331:
(make-array 4 :element-type 'integer :initial-contents '(1 2 3 4) :adjustable t)</lang>
 
=={{header|[[D}}]]==
{{works with|DMD}}
 
Line 338 ⟶ 341:
int[5] = [0,1,2,3,4];</lang>
 
=={{header|[[Delphi}}]]==
'''Defining a variable-length array'''
<lang delphi>var
Line 356 ⟶ 359:
Delphi dynamic arrays have base 0 index.
 
=={{header|[[E}}]]==
In accordance with its guarantees of determinism, you can never have an ''uninitialized'' array in E.
 
Line 368 ⟶ 371:
If you want an array of some mutable objects, see [[N distinct objects#E]].
 
=={{header|[[Forth}}]]==
Forth has a variety of ways to allocate arrays of data, though it has no built-in array handling words, favoring pointer manipulation.
 
Line 389 ⟶ 392:
0 to MyArray</lang>
 
=={{header|[[Fortran}}]]==
In ANSI FORTRAN 77 or later, this is a default-indexing array declaration:
 
Line 478 ⟶ 481:
13, 14, 15, 16 /), (/ 4, 4 /) ) )</lang>
 
=={{header|[[Groovy}}]]==
In Groovy "arrays" are synonymous with "lists", and are thus not of a fixed size. Ranges are also a type of list.
<lang groovy>
Line 487 ⟶ 490:
</lang>
 
=={{header|[[Haskell}}]]==
 
Arrays are initialized either by a list of index-value-pairs or by a list of values:
Line 501 ⟶ 504:
There are several flavours of arrays in Haskell, but creation looks very similar for the others.
 
=={{header|[[IDL}}]]==
IDL doesn't really distinguish between scalars and arrays - the same operations that can create the one can ''usually'' create the other as well.
 
Line 515 ⟶ 518:
print,a^2
9 25 64 49</lang>
=={{header|[[J}}]]==
"Creating an array" is topic seldom discussed in J as ''every'' verb takes array argument(s) and returns an array result (that is, ''everything'' creates an array). For example:
 
Line 540 ⟶ 543:
Both the <tt>a+b</tt>and the <tt>|."1 c</tt> expressions create arrays.
 
=={{header|[[Java}}]]==
For example for an array of 10 int values:
<lang java>int[] intArray = new int[10];</lang>
Line 548 ⟶ 551:
<lang java>Object[] obs = new Object[size];</lang>
 
=={{header|[[JavaScript}}]]==
<lang javascript> var myArray = new Array();
var myArray1 = new Array(5); // gotcha: preallocated array with five empty elements, not [ 5 ].
Line 554 ⟶ 557:
var myArray3 = ["Item1", "Item2"];</lang>
 
=={{header|[[Logo}}]]==
<lang logo>array 5 ; default origin is 1
(array 5 0) ; custom origin
{1 2 3 4 5} ; array literal</lang>
 
=={{header|[[LSE64}}]]==
<lang lse64>10 myArray :array</lang>
 
=={{header|[[Mathematica}}]]==
Creating an array is simply done with Set (=). An array can contain anything, and can have different Heads. A formule like 2+3x+x^2 is an array: Plus[2,Times[3,x],Times[2,Power[x,2]]]. Each list has different heads (Plus,Times, Power). Arrays are made as follows:
<lang Mathematica>
Line 572 ⟶ 575:
Note that arrays can have any dimensions, and don't have to be rectangular (every (sub)element can have different length).
 
=={{header|[[MAXScript}}]]==
{{works with|3D Studio Max|8}}
 
Line 587 ⟶ 590:
for i in 1 to 100 do arr[i] = 0</pre>
 
=={{header|[[mIRC Scripting Language}}]]==
{{works with|mIRC Script Editor}}
 
Line 593 ⟶ 596:
<lang mirc>alias creatmearray { .echo -a $array_create(MyArray, 5, 10) }</lang>
 
=={{header|[[Modula-3}}]]==
Modula-3 arrays include their range in the declaration.
<lang modula3>VAR a: ARRAY [1..10] OF INTEGER;</lang>
Line 608 ⟶ 611:
Which creates a 2 dimensional array of 10 integers each.
 
=={{header|[[Nial}}]]==
To create an array of 5 elements with values from 1 to 5
<lang nial>| count 5
Line 618 ⟶ 621:
<lang nial>| newarr := [2 4 6]</lang>
 
=={{header|[[Oberon-2}}]]==
Create an array of 10 integers. Initial values are undefined. All arrays are zero-indexed.
<lang oberon2>VAR a: ARRAY 10 OF INTEGER;</lang>
Line 634 ⟶ 637:
creates a 3 dimensional array of 10 integers each.
 
=={{header|[[OCaml}}]]==
Using an array literal:
 
Line 653 ⟶ 656:
let array = Array.init 5 callback</lang>
 
=={{header|[[Pascal}}]]==
'''Using a fixed length array as global or stack variable'''
<lang pascal>var
Line 698 ⟶ 701:
Note that also the lower index may be given as dynamic argument.
 
=={{header|[[Perl}}]]==
{{works with|Perl|5}}
<lang perl> my @empty;
Line 733 ⟶ 736:
</lang>
 
=={{header|[[PHP}}]]==
 
For a single dimension array with 10 elements:
Line 767 ⟶ 770:
?></lang>
 
=={{header|[[Pike}}]]==
For a single dimension int array:
<lang pike> array(int) x = ({ 1, 2, 3 });</lang>
Line 779 ⟶ 782:
Note that inner arrays can be of different sizes, as are simply values of the outer array.
 
=={{header|[[Pop11}}]]==
Pop11 distinguishes between vectors and arrays. Vectors are one dimensional and the lowest index is 1. There is special shorthand syntax to create vectors:
 
Line 796 ⟶ 799:
vars a1 = newarray([2 5 -1 1], 0);</lang>
 
=={{header|[[Python}}]]==
List are mutable arrays. You can put anything into a list, including other lists.
 
Line 835 ⟶ 838:
The [http://docs.python.org/library/ctypes.html#arrays ctypes module] can construct specialised arrays for interfacing with external C functions. The [http://docs.python.org/library/struct.html struct module] allows a string of characters to be interpreted as an array of values of C types such as 32 bit ints, float, etc.
 
=={{header|[[R}}]]==
<lang R>a <- vector("numeric", 10) # a is a vector (array) of 10 numbers
b <- vector("logical", 10) # b is an array of logical (boolean) values
Line 854 ⟶ 857:
 
 
=={{header|[[Raven}}]]==
<lang raven>[ 1 2 3.14 'a' 'b' 'c' ] as a_list
a_list print</lang>
Line 866 ⟶ 869:
5 => "c"
 
=={{header|[[Ruby}}]]==
This is the most basic way to create an empty one-dimensional array in Ruby:
<lang ruby> my_array = Array.new
Line 910 ⟶ 913:
array = Array.new(4) {|i| Array.new(6,i)}</lang>
 
=={{header|[[Scala}}]]==
<lang scala> val array = new Array[int](10) // a 10 element array
val stringArray = new Array[String](20) // a 20 element string array
Line 917 ⟶ 920:
(1 :: 2 :: 3 :: 4 :: 5 :: Nil).toArray</lang>
 
=={{header|[[Scheme}}]]==
Lists are more often used in Scheme than vectors.
 
Line 931 ⟶ 934:
 
<lang scheme> (make-vector 5 0)</lang>
=={{header|[[Script3D}}]]==
Script3D has dynamic arrays allowing to store any datatype and a special type for storing vectors of floating point values, typically used in 3D applications.
 
Line 940 ⟶ 943:
var myVector = Vector(2000); // 2000 elements vector of floats</lang>
 
=={{header|[[Slate}}]]==
<lang slate>
define: #array -> (Array newSize: 20).
Line 949 ⟶ 952:
</lang>
 
=={{header|[[Smalltalk}}]]==
<lang smalltalk>|array|
"creates an array that holds up to 20 elements"
Line 963 ⟶ 966:
array at: 2 put: 'orange'.</lang>
 
=={{header|[[Standard ML}}]]==
Converting from a list:
 
Line 982 ⟶ 985:
<lang sml> val vector = #[1,2,3,4,5]</lang>
 
=={{header|[[Tcl}}]]==
 
Tcl uses the <tt>list</tt> for what many other languages call "array". A list is an ordered, numerically indexable (zero based) collection of values in a single variable. Each list entry itself can be a list.
Line 1,002 ⟶ 1,005:
this creates an array with the name <tt>b</tt> with two elements. The keys of the elements are "foo" and "bar" and the values are <tt>b(foo) == 12</tt> and <tt>b(bar) == hello</tt>.
 
=={{header|[[Toka}}]]==
Toka allows creation of an array using is-array. Access to the elements is done using get-element, put-element, get-char-element, and put-char-element functions. You can not initialize the values automatically using the core array functions.
 
Line 1,008 ⟶ 1,011:
100 chars is-array bar</lang>
 
=={{header|[[Visual Basic .NET}}]]==
 
Implicit Size
Line 1,039 ⟶ 1,042:
<lang vbnet>Dim words() AS String = 'perl style'.split(" "c) ' You must tell VB that the space is a character by denoting c after the " "</lang>
 
=={{header|[[VBScript}}]]==
 
<lang vbscript>Dim myArray(2)
Anonymous user