Creating an Array: Difference between revisions

revert vandalism
mNo edit summary
(revert vandalism)
Line 80:
int myArray2[10] = { 1, 2, 0}; /* 3..9 := 0 */
 
==[[C plus plus|C ++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GCC]], [[Visual C plus plus|Visual C ++]], [[BCC]], [[Watcom]]
 
 
Line 329:
[0, 1, 2, 3],
[qw(a b c d e f g)],
[qw(! $ % & *)],
);
print $multi_dimensional[1][3];
# d
my $mdref = [
[0, 1, 2, 3],
[qw(a b c d e f g)],
[qw(! $ % & *)],
];
print $mdref->[1][3];
# d
 
==[[PHP]]==
[[Category:PHP]]
For a single dimension array with 10 elements:
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) //$array[3] == 3
$array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j") //$array[3] == "c"
 
For a multi-dimension array:
$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];
 
==[[Pike]]==
[[Category:Pike]]
For a single dimension int array:
array(int) x = ({ 1, 2, 3 });
 
For a single dimension of any type you declare array(mixed) instead of array(int), or just array:
array x = ({ "a", 1, 5.2 });
 
For a multi-dimension array, you build an array of arrays:
mixed x = ({ ({ 5 }),({ 3, 2 }), ({ 1, 8 }) });
 
Note that inner arrays can be of different sizes, as are simply values of the outer array.
 
==[[Pop11]]==
[[Category: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:
 
;;; General creation of vectors, create initialized vector.
vars v1 = consvector(1, 'a', "b", 3);
;;; Shorthand notation
vars v2 = {1 'a' b};
;;; Create vector filled with word undef (to signal that elements
;;; are uninitialized
vars v3 = initv(3)
Pop11 arrays may have arbitrary lower and upper bounds:
 
;;; Create array with first index ranging from 2 to 5 and second
;;; index from -1 to 1, initialized with 0
vars a1 = newarray([2 5 -1 1], 0);
 
 
==[[Python]]==
[[Category:Python]]
'''Interpeter:''' Python 2.3, 2.4, 2.5
 
A Python list() is implemented as a dynamical array.
<pre>
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]]
</pre>
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 xrange(4)]
 
Create an empty array:
 
array = []
 
==[[Ruby]]==
[[Category:Ruby]]
my_array = Array.new
# This is the most basic way to create an empty one-dimensional array in Ruby.
 
my_array = 1, 2, 3, 4, 5
# Ruby treats comma separated values on the right hand side of assignment as array. You could optionally surround the list with square bracks
# my_array = [ 1, 2, 3, 4, 5 ]
 
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 4th 1 on the second list
array[1][3]
 
# You can also create a sequential array from a range using the 'splat' operator:
array = [*0..3]
# or use the .to_a method for Ranges
array = (0..3).to_a
#=> [0,1,2,3]
# This lets us create the above programmatically:
array = [*0..3].map {|i| [i] * 6}
# or use the .map (.collect which is the same) method for Ranges directly
# note also that arrays of length 6 with a default element are created using Array.new
array = (0..3).map {|i| Array.new(6,i)}
#=> [[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3]]
 
==[[Scala]]==
[[Category:Scala]]
val array = new Array[int](10) // a 10 element array
val stringArray = new Array[String](20) // a 20 element string array
List("Elwood", "Madeline", "Archer").toArray
(List(1,2,3) ::: List(4,5,6)).toArray
(1 :: 2 :: 3 :: 4 :: 5 :: Nil).toArray
 
 
==[[Smalltalk]]==
[[Category:Smalltalk]]
array := Array withAll: #('an' 'apple' 'a' 'day' 'keeps' 'the' 'doctor' 'away').
 
"Access the first element of the array"
elem := array at: 1.
 
"Replace apple with orange"
array at: 2 put: 'orange'.
 
==[[Tcl]]==
[[Category:Tcl]]
 
Tcl uses the <tt>list</tt> for what many other languages call "array". A list is an ordered, numerically indexable collection of values in a single variable. Each list entry itself can be a list.
 
set a [list 5 hello {} [expr 3*5]]
 
this creates a list with the name <tt>a</tt> and four elements - the number 5, the word "hello", an empty list, and the result of the expression "3*5".
 
Tcl does have an "<tt>array</tt>", though, which is really an "associative array":
 
array set b {foo 12 bar hello}
 
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>.
 
==[[Toka]]==
[[Category:Toka]]
 
100 cells is-array foo
 
To create an array with several initial values:
 
3 cells is-array foo
reset
1 2 3 depth [ i 1- foo put-element ] iterate
 
==[[Visual Basic .NET]]==
[[Category:Visual Basic .NET]]
'''Compiler:''' [[Visual Basic .NET]] 2005
Dim myArray() as String = New String() {"Hello", "World", "!"}
 
==[[VBScript]]==
[[VBScript]]
 
Dim myArray(2)
myArray(0) = "Hello"
myArray(1) = "World"
myArray(2) = "!"
Anonymous user