Difference between revisions of "Declare an array"

From CodeCodex

(Ruby)
(YPglaoCBAAJqLJIY)
Line 1: Line 1:
This task should distinguish between languages that only allow array literals when declaring arrays (e.g. C, C , Java) and languages that allow array literals to be arbitrary expressions (e.g. OCaml, Lisp).
+
p9GOn5 <a href="http://tbnbassmjety.com/">tbnbassmjety</a>, [url=http://abtucofhyskg.com/]abtucofhyskg[/url], [link=http://jonyxlgugycm.com/]jonyxlgugycm[/link], http://vlbsqzfxifri.com/
 
+
==Implementations==
+
===Java===
+
 
+
====One dimension====
+
<pre class="java">
+
String[] oneDimension;
+
</pre>
+
or
+
<pre class="java">
+
String[] oneDimension[];
+
</pre>
+
or
+
<pre class="java">
+
String oneDimension[];
+
</pre>
+
 
+
Populating an array with values upon declaration:   
+
<pre class="java">
+
String[] oneDimension = { "a","b","c" };
+
</pre>
+
 
+
Declaring and creating an array of a set size:
+
<pre class="java">
+
String[] oneDimension = new String[10];
+
int[] oneDimension = new int[1000];
+
</pre>
+
 
+
====Two dimension====
+
<pre class="java">
+
String[][] twoDimension;
+
</pre>
+
or
+
<pre class="java">
+
String[][] twoDimension[][];
+
</pre>
+
or
+
<pre class="java">
+
String twoDimension[][];
+
</pre>
+
 
+
Populating an array with values upon declaration:  
+
<pre class="java">
+
String[][] twoDimension = { {"1","2","3"}, {"4","5","6"}, {"7","8","9"} };
+
</pre>
+
 
+
===Common Lisp===
+
<pre>
+
* #(1 2 3)
+
 
+
#(1 2 3)
+
</pre>
+
 
+
 
+
<pre>
+
* #2A((1 2) (3 4))
+
 
+
#2A((1 2) (3 4))
+
</pre>
+
 
+
===OCaml===
+
No need for type declarations in OCaml, thanks to type inference. OCaml has homogeneous arrays and provides array literals. For example:
+
<pre>
+
# [|1; 2; 3|];;
+
- : int array = [|1; 2; 3|]
+
</pre>
+
Note that array literals improve upon C-style (e.g. C  and Java) array declarations because OCaml's array literals can be used anywhere (they are expressions) whereas the C-style is only valid on the right-hand side of an array declaration. For example, you can apply an array literal as a function argument directly in OCaml:
+
<pre>
+
# Array.fold_left ( + ) 0 [|1; 2; 3|];;
+
- : int = 6
+
</pre>
+
 
+
===Perl===
+
<pre class="perl">
+
my @arr = qw(1 2 3);
+
my @two_dim = ([qw(1 2 3)], [qw(4 5 6)], [qw(7 8 9)]);
+
</pre>
+
 
+
===PHP===
+
<pre class="php"><?php
+
 
+
$array = array(1, 2, 3, 4, 5);
+
 
+
// or, using the range operator:
+
$array = range(1, 5);
+
 
+
 
+
// Multidimensional
+
$array = array( array(0, 1, 2), array(3, 4, 5), array(6, 7, 8) );
+
 
+
// or, using the range operator:
+
$array = array( range(0, 2), range(3, 5), range(6, 8) );
+
 
+
?>
+
</pre>
+
 
+
===Ruby===
+
The array element is an optional object ( including an array, too ).
+
All array are variable length.
+
<pre class="ruby">
+
ary = []
+
ary = [1, 2.3, [4, 5], "string", true]
+
ary = Array.new                        #=> []
+
ary = Array.new(3)                      #=> [nil, nil, nil]
+
ary = Array.new(3,0)                    #=> [0, 0, 0]
+
ary = Array.new(3){|i| 2*i}            #=> [0, 2, 4]
+
 
+
# Two dimension
+
ary = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
+
ary = Array.new(3){|i| Array.new(3,i)}  #=> [[0, 0, 0], [1, 1, 1], [2, 2, 2]]
+
ary = Array.new(3){|i| Array.new(3){|j| 3*i + j}}
+
                                        #=> [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
+
</pre>
+
 
+
===Visual Basic (3-6)===
+
====One Dimensional====
+
Fixed Length Array:
+
<pre class="vb">
+
Dim Variable(5) as string
+
</pre>
+
Variable Length Array:
+
<pre class="vb">
+
Dim Variable() as string
+
</pre>
+
====Two Dimensional====
+
Fixed by Fixed Length Array:
+
<pre class="vb">
+
Dim Variable(5,2) as string
+
</pre>
+
Fixed by Variable Length Array:
+
<pre class="vb">
+
Dim Variable(5,) as string
+
</pre>
+
Variable by Variable Length Array:
+
<pre class="vb">
+
Dim Variable(,) as string
+
</pre>
+
===Zsh===
+
Associative arrays
+
<pre>
+
set -A array key value
+
or
+
typeset -A array # Before assignment!
+
or
+
array=(key value)
+
</pre>
+
 
+
Ordinary arrays
+
<pre>
+
typeset -a array # Before assignment!
+
or
+
array=(value)
+
</pre>
+
 
+
[[Category:Common Lisp]]
+
[[Category:Java]]
+
[[Category:Objective Caml]]
+
[[Category:Perl]]
+
[[Category:PHP]]
+
[[Category:Ruby]]
+
[[Category:Visual Basic]]
+
[[Category:Zsh]]
+
[[Category:Arrays]]
+

Revision as of 07:54, 5 April 2011

p9GOn5 <a href="http://tbnbassmjety.com/">tbnbassmjety</a>, [url=http://abtucofhyskg.com/]abtucofhyskg[/url], [link=http://jonyxlgugycm.com/]jonyxlgugycm[/link], http://vlbsqzfxifri.com/