Arrays: Difference between revisions

3,953 bytes added ,  21 days ago
m
m (→‎{{header|Tailspin}}: indexes start at any number)
 
(13 intermediate revisions by 9 users not shown)
Line 730:
<syntaxhighlight lang="ada">procedure Array_Test is
 
A,type BExample_Array_Type :is array (1..20) of Integer;
A, B : Example_Array_Type;
 
-- Ada array indices may begin at any value, not just 0 or 1
C : array (-37..20) of integerInteger;
 
-- Ada arrays may be indexed by enumerated types, which are
Line 756 ⟶ 757:
Centered : Arr (-50..50) := (0 => 1, Others => 0);
 
Result : Integer;
begin
 
A := (others => 0); -- Assign whole array
B := (1 => 1, 2 => 1, 3 => 2, others => 0);
-- Assign whole array, different values
A (1) := -1; -- Assign individual element
Line 766 ⟶ 767:
A (3..5) := (2, 4, -1); -- Assign a constant slice
A (3..5) := A (4..6); -- It is OK to overlap slices when assigned
 
Fingers_Extended(Fingers'First) := False; -- Set first element of array
Fingers_Extended(Fingers'Last) := False; -- Set last element of array
 
end Array_Test;</syntaxhighlight>
Line 3,005 ⟶ 3,006:
void run() {
// an array literal has Constant mutability; it is **not** mutable
immutable Int[] literalArray = [1,2,3];
show($"{literalArray=}, {&literalArray.actualType=}");
 
Line 3,013 ⟶ 3,014:
// modifications to a Constant array result in a new Constant array;
// in Computer Science, this is called a persistent data structure
immutable Int[] biggerArray = literalArray + 4;
show($"{biggerArray=}, {&biggerArray.actualType=}");
 
immutable Int[] biggestArray = biggerArray + biggerArray;
show($"{biggestArray=}, {&biggestArray.actualType=}");
 
Line 3,222 ⟶ 3,223:
 
=={{header|Elena}}==
ELENA 56.0x:
 
Static array
<syntaxhighlight lang="elena"> var staticArray := new int[]{1, 2, 3};</syntaxhighlight>
Generic array
<syntaxhighlight lang="elena"> var array := system'Array.allocate:(3);
array[0] := 1;
array[1] := 2;
Line 3,238 ⟶ 3,239:
Dynamic array
<syntaxhighlight lang="elena"> var dynamicArray := new system'collections'ArrayList();
dynamicArray.append:(1);
dynamicArray.append:(2);
dynamicArray.append:(4);
 
dynamicArray[2] := 3;</syntaxhighlight>
Line 3,871 ⟶ 3,872:
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1, @"FutureBasic Arrays"
window 1, @"FutureBasic Arrays", (0,0,480,450)
 
begin globals
Line 3,931 ⟶ 3,933:
gA1(10) = 67
for i = 0 to fn DynamicNextElement( dynamic(gA1) ) - 1
print gA1(i),
next
print
 
gA1(5) = 19
gA1(10) = 22
for i = 0 to fn DynamicNextElement( dynamic(gA1) ) - 1
print gA1(i),
next
print
 
gA2(0) = "Kilo"
gA2(1) = "Lima"
gA2(5) = "Mike"
for i = 0 to fn DynamicNextElement( dynamic(gA2) ) - 1
print gA2(i),
next
print
 
gA2(1) = "November"
gA2(6) = "Oscar"
for i = 0 to fn DynamicNextElement( dynamic(gA2) ) - 1
print gA2(i),
next
print : print
end fn
 
Line 4,035 ⟶ 4,037:
for i = 0 to len(a1) - 1
print a1[i],
next
print : print
end fn
 
void local fn FB_MDA
long i
text ,, fn ColorGray
print @"// FB MDA - mutable, dynamic, multi-dimensional"
text
mda_add = @"Alpha"
mda_add = @"Romeo"
mda_add = @"Mike"
for i = 0 to mda_count - 1
print mda(i),
next
print
mda_swap(0),(2)
mda(1) = @"Delta"
for i = 0 to mda_count - 1
print mda(i),
next
end fn
Line 4,043 ⟶ 4,072:
fn CoreFoundationMutableFixedLength
fn CoreFoundationMutableDynamic
fn FB_MDA
 
HandleEvents</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 4,073 ⟶ 4,104:
Juliet Golf India
Foxtrot Golf Hotel
 
// FB MDA - mutable, dynamic, multi-dimensional
Alpha Romeo Mike
Mike Delta Alpha
</pre>
 
Line 4,676 ⟶ 4,711:
print(d[1])
}</syntaxhighlight>
 
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">> (var my-list [1 2 3 4 5]) ;syntactic sugar
[1 2 3 4 5]
 
> (var my-list (vec 1 2 3 4 5))
[1 2 3 4 5]
 
> my-list
[1 2 3 4 5]
 
> (3 my-list) ;fourth element
4
 
> (-1 my-list) ;last element
5
 
> (append my-list 100)
[1 2 3 4 5 100]
 
> my-list ;variables are immutable so my-list cannot be changed without being redefined
[1 2 3 4 5]</syntaxhighlight>
 
=={{header|Io}}==
Line 5,213 ⟶ 5,270:
println: foo popFront. ;; "front"
println: foo. ;; [1, 99]</syntaxhighlight>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
myArray is list of numbers
 
procedure:
# add elements
push 1 to myArray
push 2 to myArray
push 3 to myArray
 
# access elements
display myArray:0 lf
 
# store elements
store 99 in myArray:0
 
# remove elements
remove element at 1 from myArray
delete last element of myArray
 
# clear array
clear myArray
</syntaxhighlight>
 
=={{header|LFE}}==
Line 6,037 ⟶ 6,118:
FunctionEnd
</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
let x = [1 2 3]
 
print $x
 
# Both are equivalent
print $x.1 ($x | get 1)
 
# Shadowing the original x
let x = $x | append 4
print $x
 
# Using mut
mut y = [a b c]
print $y
$y = $y | append d
print $y
</syntaxhighlight>
{{out}}
<pre>
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
╰───┴───╯
 
2
2
╭───┬───╮
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 3 │
│ 3 │ 4 │
╰───┴───╯
 
╭───┬───╮
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
╰───┴───╯
 
╭───┬───╮
│ 0 │ a │
│ 1 │ b │
│ 2 │ c │
╰───┴───╯
</pre>
 
=={{header|Oberon-2}}==
Line 7,738 ⟶ 7,868:
/end-free
</syntaxhighlight>
 
{{works with|ILE RPG v7.1+}}
<nowiki>**</nowiki>free
//-Static array
//--def of 10 el array of integers, initialised to zeros
dcl-s array int(10) dim(10) inz;
//--def an el
dcl-s el_1 int(10) inz(0);
//-assign first el
//--first element of RPG array is indexed with 1
array(1) = 111;
//-get first el of array
el_1 = array(1);
//--display it
dsply ('First el of array='+%char(el_1));
//--displays: First el of array=111
//---or shorter, without "el_1"
dsply ('First el of array='+%char(array(1)));
//--displays: First el of array=111
 
=={{header|RPL}}==
Line 8,321 ⟶ 8,474:
slate[7]> x at: 0.
1</syntaxhighlight>
 
=={{header|SmallBASIC}}==
<syntaxhighlight lang="SmallBASIC">
' One dimensional arrays
DIM A ' empty array
DIM B(3) ' empty array with 4 elements
DIM C(2 TO 4) ' empty array with elements 2,3 and 4
D = [1,2,3,4] ' assign array in one statement
E = ["one", "two", "three"] ' string array
F = [1, "two", [1,2,3]] ' arrays can contain mixed data types
 
B[0] = 1 ' use [] or () to assign value to
B(1) = 2 ' element or access elements
 
A << 2 ' append element to an array
 
print F ' print whole array -> Output: [1,two,[1,2,3]]
print F[0] ' print first element -> Output: 1
print F(1) ' print second element -> Output: two
 
' Multi dimensional arrays
DIM A(2,0) ' column array (vector) with 3 elements
DIM B(2,2) ' empty 2D array (matrix) with 3x3 elements
DIM C(2,2,2) ' empty 3D array with 3x3x3 elements
 
A[0,0] = 1
A[1,0] = 2
A[2,0] = 3
 
' Math with arrays
 
A = [1,2,3]
B = [4,5,6]
 
print A + B ' Output: [5,7,9]
print 3 * A ' Output: [3,6,9]
print A * B ' Output: [4,10,18]
 
C = [1;2;3] ' vector
D = [1,2,3;4,5,6;7,8,9] ' 2D matrix
 
print D * C ' matrix * vector -> Output [14;32;50]
print D * D ' matrix * matrix -> Output [30,36,42;66,81,96;102,126,150]
</syntaxhighlight>
 
 
=={{header|Smalltalk}}==
Line 9,184 ⟶ 9,382:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var arr = []
arr.add(1)
arr.add(2)
2

edits