Difference between revisions of "Declare an array"

From CodeCodex

(Java)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
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===
DECALRING:
+
  
1)type[] name;  
+
====One dimension====
int [] number;
+
<pre class="java">
 +
String[] oneDimension;
 +
</pre>
 +
or
 +
<pre class="java">
 +
String oneDimension[];
 +
</pre>
  
SAMPLE OF ARRAY:  
+
Populating an array with values upon declaration:  
 +
<pre class="java">
 +
String[] oneDimension = { "a","b","c" };
 +
</pre>
  
number=new int[5];
+
Declaring and creating an array of a set size:
 +
<pre class="java">
 +
String[] oneDimension = new String[10];
 +
int[] oneDimension = new int[1000];
 +
</pre>
  
2)int[]number=new int[5];
+
====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]]

Latest revision as of 05:30, 20 March 2012

Implementations[edit]

Java[edit]

One dimension[edit]

 String[] oneDimension;

or

 String oneDimension[];

Populating an array with values upon declaration:

 String[] oneDimension = { "a","b","c" };

Declaring and creating an array of a set size:

 String[] oneDimension = new String[10];
 int[] oneDimension = new int[1000];

Two dimension[edit]

 String[][] twoDimension;

or

 String[] twoDimension[];

or

 String twoDimension[][];

Populating an array with values upon declaration:

 String[][] twoDimension = { {"1","2","3"}, {"4","5","6"}, {"7","8","9"} };

Common Lisp[edit]

* #(1 2 3)

#(1 2 3)


* #2A((1 2) (3 4))

#2A((1 2) (3 4))

OCaml[edit]

No need for type declarations in OCaml, thanks to type inference. OCaml has homogeneous arrays and provides array literals. For example:

# [|1; 2; 3|];;
- : int array = [|1; 2; 3|]

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:

# Array.fold_left ( + ) 0 [|1; 2; 3|];;
- : int = 6

Perl[edit]

my @arr = qw(1 2 3);
my @two_dim = ([qw(1 2 3)], [qw(4 5 6)], [qw(7 8 9)]);

PHP[edit]

<?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) );

?>

Ruby[edit]

The array element is an optional object ( including an array, too ). All array are variable length.

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]]

Visual Basic (3-6)[edit]

One Dimensional[edit]

Fixed Length Array:

 Dim Variable(5) as string

Variable Length Array:

 Dim Variable() as string

Two Dimensional[edit]

Fixed by Fixed Length Array:

 Dim Variable(5,2) as string

Fixed by Variable Length Array:

 Dim Variable(5,) as string

Variable by Variable Length Array:

 Dim Variable(,) as string

Zsh[edit]

Associative arrays

set -A array key value
or
typeset -A array # Before assignment!
or
array=(key value)

Ordinary arrays

typeset -a array # Before assignment!
or
array=(value)