Creating an Array: Difference between revisions
Content added Content deleted
(→Smalltalk: Delete.) |
(Partial merge to Arrays per deprecated tag) |
||
Line 6: | Line 6: | ||
In this task, the goal is to create an [[array]]. Mention if the [[array base]] begins at a number other than zero. |
In this task, the goal is to create an [[array]]. Mention if the [[array base]] begins at a number other than zero. |
||
In addition, demonstrate how to initialize an array variable with data. |
In addition, demonstrate how to initialize an array variable with data. |
||
==[[Ada]]== |
|||
'''Compiler:''' [[GCC]] 4.1.2 |
|||
Ada array indices may begin at any value, not just 0 or 1 |
|||
<lang ada> type Arr is array (Integer 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); |
|||
Centered : Arr (-50..50) := (0 => 1, Others => 0); |
|||
</lang> |
|||
Ada arrays may be indexed by enumerated types, which are discrete non-numeric types |
|||
<lang ada> type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); |
|||
type Activities is (Work, Fish); |
|||
type Daily_Activities is array(Days) of Activities; |
|||
This_Week : Daily_Activities := (Mon..Fri => Work, Others => Fish); |
|||
</lang> |
|||
==[[ALGOL 68]]== |
==[[ALGOL 68]]== |
||
Line 83: | Line 65: | ||
BITS byte := (TRUE, TRUE); |
BITS byte := (TRUE, TRUE); |
||
SKIP</lang> |
SKIP</lang> |
||
==[[AmigaE]]== |
|||
<lang amigae>DEF ai[100] : ARRAY OF CHAR, -> static |
|||
da: PTR TO CHAR, |
|||
la: PTR TO CHAR |
|||
PROC main() |
|||
da := New(100) |
|||
-> or |
|||
NEW la[100] |
|||
IF da <> NIL |
|||
ai[0] := da[0] -> first is 0 |
|||
ai[99] := da[99] -> last is "size"-1 |
|||
Dispose(da) |
|||
ENDIF |
|||
-> using NEW, we must specify the size even when |
|||
-> "deallocating" the array |
|||
IF la <> NIL THEN END la[100] |
|||
ENDPROC</lang> |
|||
==[[AppleScript]]== |
|||
AppleScript arrays are called lists: |
|||
<lang applescript> set empty to {} |
|||
set ints to {1, 2, 3}</lang> |
|||
Lists can contain any objects including other lists: |
|||
<lang applescript> set any to {1, "foo", 2.57, missing value, ints}</lang> |
|||
==[[AutoHotkey]]== |
==[[AutoHotkey]]== |
||
Line 121: | Line 76: | ||
==[[BASIC]]== |
==[[BASIC]]== |
||
{{works with|QuickBasic|4.5}} |
|||
{{works with|PB|7.1}} |
|||
The default array base (lower bound) can be set with OPTION BASE. If OPTION BASE is not set, the base may be either 0 or 1, depending on implementation. The value given in DIM statement is the upper bound. If the base is 0, then DIM a(100) will create an array containing 101 elements. |
|||
<lang qbasic> OPTION BASE 1 |
|||
DIM myArray(100) AS INTEGER </lang> |
|||
Alternatively, the lower and upper bounds can be given while defining the array: |
|||
<lang qbasic> DIM myArray(-10 TO 10) AS INTEGER </lang> |
|||
Dynamic arrays: |
|||
<lang qbasic> 'Specify that the array is dynamic and not static: |
|||
'$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" </lang> |
|||
'''Array Initialization''' |
|||
Arrays are initialized to zero or zero length strings when created. |
|||
BASIC does not generally have option for initializing arrays to other values, so the initializing is usually done at run time. |
|||
DATA and READ statements are often used for this purpose: |
|||
<lang qbasic> DIM month$(12) |
|||
DATA January, February, March, April, May, June, July |
|||
DATA August, September, October, November, December |
|||
FOR m=1 TO 12 |
|||
READ month$(m) |
|||
NEXT m </lang> |
|||
{{works with|FreeBASIC}} |
|||
FreeBASIC has an option to initialize array while declaring it. |
|||
<lang freebasic> Dim myArray(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}} </lang> |
|||
==[[C++]]== |
==[[C++]]== |
||
<nowiki>{{libheader|Qt}}</nowiki> |
<nowiki>{{libheader|Qt}}</nowiki> |
||
<lang cpp> // Qt |
|||
QVector<int> myArray4(10); |
|||
myArray4.push_back(1); |
|||
myArray4.push_back(2);</lang> |
|||
<nowiki>{{libheader|MFC}}</nowiki> |
|||
<lang cpp> // MFC |
|||
CArray<int,int> myArray5(10); |
|||
myArray5.Add(1); |
|||
myArray5.Add(2);</lang> |
|||
==[[C sharp|C#]]== |
==[[C sharp|C#]]== |
||
Example of array of 10 int types: |
|||
<lang csharp> int[] numbers = new int[10];</lang> |
|||
Example of array of 3 string types: |
|||
<lang csharp> string[] words = { "these", "are", "arrays" };</lang> |
|||
You can also declare the size of the array and initialize the values at the same time: |
|||
<lang csharp> int[] more_numbers = new int[3]{ 21, 14 ,63 };</lang> |
|||
For Multi-Dimensional arrays you declare them the same except for a comma in the type declaration. |
|||
The following creates a 3x2 int matrix |
|||
<lang csharp> int[,] number_matrix = new int[3,2];</lang> |
|||
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. |
|||
<lang csharp> string[,] string_matrix = { {"I","swam"}, {"in","the"}, {"freezing","water"} };</lang> |
|||
or |
|||
<lang csharp> string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };</lang> |
|||
==[[Clean]]== |
|||
Array denotations are overloaded in Clean, therefore we explicitly specify the types. There are lazy, strict, and unboxed array. |
|||
===Lazy array=== |
|||
Create a lazy array of strings using an array denotation. |
|||
<lang clean>array :: {String} |
|||
array = {"Hello", "World"}</lang> |
|||
Create a lazy array of floating point values by sharing a single element. |
|||
<lang clean>array :: {Real} |
|||
array = createArray 10 3.1415</lang> |
|||
Create a lazy array of integers using an array (and also a list) comprehension. |
|||
<lang clean>array :: {Int} |
|||
array = {x \\ x <- [1 .. 10]}</lang> |
|||
===Strict array=== |
|||
Create a strict array of integers. |
|||
<lang clean>array :: {!Int} |
|||
array = {x \\ x <- [1 .. 10]}</lang> |
|||
===Unboxed array=== |
|||
Create an unboxed array of characters, also known as <tt>String</tt>. |
|||
<lang clean>array :: {#Char} |
|||
array = {x \\ x <- ['a' .. 'z']}</lang> |
|||
==[[ColdFusion]]== |
|||
Creates a one-dimensional Array |
|||
<lang coldfusion><cfset arr1 = ArrayNew(1)></lang> |
|||
Creates a two-dimensional Array in CFScript |
|||
<lang coldfusion><cfscript> |
|||
arr2 = ArrayNew(2); |
|||
</cfscript></lang> |
|||
''ColdFusion Arrays are '''NOT''' zero-based, they begin at index '''1''''' |
|||
==[[Common Lisp]]== |
==[[Common Lisp]]== |
||
Creates a one-dimensional array of length 10. The initial contents are undefined. |
|||
<lang lisp>(make-array 10)</lang> |
|||
Creates a two-dimensional array with dimensions 10x20. |
|||
<lang lisp>(make-array '(10 20))</lang> |
|||
<tt>make-array</tt> may be called with a number of optional arguments. |
|||
<lang lisp>; Makes an array of 20 objects initialized to nil |
|||
(make-array 20 :initial-element nil) |
|||
; Makes an integer array of 4 elements containing 1 2 3 and 4 initially which can be resized |
|||
(make-array 4 :element-type 'integer :initial-contents '(1 2 3 4) :adjustable t)</lang> |
|||
==[[D]]== |
==[[D]]== |