Collections: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 468: | Line 468: | ||
Arbitrarily complex data structures can be constructed, normally via language features <code>struct</code> and pointers. They are everywhere, but not provided by the C language itself per se. |
Arbitrarily complex data structures can be constructed, normally via language features <code>struct</code> and pointers. They are everywhere, but not provided by the C language itself per se. |
||
=={{header|C sharp|C#}}== |
|||
===Arrays=== |
|||
<lang csharp> |
|||
// Creates and initializes a new integer Array |
|||
int[] intArray = new int[5] { 1, 2, 3, 4, 5 }; |
|||
//same as |
|||
int[] intArray = new int[]{ 1, 2, 3, 4, 5 }; |
|||
//same as |
|||
int[] intArray = { 1, 2, 3, 4, 5 }; |
|||
//Arrays are zero-based |
|||
string[] stringArr = new string[5]; |
|||
stringArr[0] = "string"; |
|||
</lang> |
|||
===ArrayList and List=== |
|||
The size of ArrayList is dynamically increased as required. ArrayLists are zero-based. |
|||
<lang csharp> |
|||
//Create and initialize ArrayList |
|||
ArrayList myAl = new ArrayList { "Hello", "World", "!" }; |
|||
//Create ArrayList and add some values |
|||
ArrayList myAL = new ArrayList(); |
|||
myAL.Add("Hello"); |
|||
myAL.Add("World"); |
|||
myAL.Add("!"); |
|||
</lang> |
|||
The List class is the generic equivalent of the ArrayList class. |
|||
A List is a strongly typed list of objects that can be accessed by index ( zero-based again). |
|||
<lang csharp> |
|||
//Create and initialize List |
|||
List<string> myList = new List<string> { "Hello", "World", "!" }; |
|||
//Create List and add some values |
|||
List<string> myList2 = new List<string>(); |
|||
myList2.Add("Hello"); |
|||
myList2.Add("World"); |
|||
myList2.Add("!"); |
|||
</lang> |
|||
===Hashtable and Dictionary=== |
|||
Hashtables represent a collection of key/value pairs that are organized based on the hash code of the key. |
|||
Keys must be unique. |
|||
<lang csharp> |
|||
//Create an initialize Hashtable |
|||
Hashtable myHt = new Hashtable() { { "Hello", "World" }, { "Key", "Value" } }; |
|||
//Create Hashtable and add some Key-Value pairs. |
|||
Hashtable myHt2 = new Hashtable(); |
|||
myHt2.Add("Hello", "World"); |
|||
myHt2.Add("Key", "Value"); |
|||
</lang> |
|||
Dictionary is a generic class.It represents a collection of key/value pairs. Keys must be unique. |
|||
<lang csharp> |
|||
//Create an initialize Dictionary |
|||
Dictionary<string, string> dict = new Dictionary<string, string>() { { "Hello", "World" }, { "Key", "Value" } }; |
|||
//Create Dictionary and add some Key-Value pairs. |
|||
Dictionary<string, string> dict2 = new Dictionary<string, string>(); |
|||
dict2.Add("Hello", "World"); |
|||
dict2.Add("Key", "Value"); |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 536: | Line 601: | ||
m.insert(7); // insert a 7 (automatically placed after the 5) |
m.insert(7); // insert a 7 (automatically placed after the 5) |
||
m.insert(5); // insert a second 5 (now m contains two 5s, followed by one 7)</lang> |
m.insert(5); // insert a second 5 (now m contains two 5s, followed by one 7)</lang> |
||
=={{header|C sharp|C#}}== |
|||
===Arrays=== |
|||
<lang csharp> |
|||
// Creates and initializes a new integer Array |
|||
int[] intArray = new int[5] { 1, 2, 3, 4, 5 }; |
|||
//same as |
|||
int[] intArray = new int[]{ 1, 2, 3, 4, 5 }; |
|||
//same as |
|||
int[] intArray = { 1, 2, 3, 4, 5 }; |
|||
//Arrays are zero-based |
|||
string[] stringArr = new string[5]; |
|||
stringArr[0] = "string"; |
|||
</lang> |
|||
===ArrayList and List=== |
|||
The size of ArrayList is dynamically increased as required. ArrayLists are zero-based. |
|||
<lang csharp> |
|||
//Create and initialize ArrayList |
|||
ArrayList myAl = new ArrayList { "Hello", "World", "!" }; |
|||
//Create ArrayList and add some values |
|||
ArrayList myAL = new ArrayList(); |
|||
myAL.Add("Hello"); |
|||
myAL.Add("World"); |
|||
myAL.Add("!"); |
|||
</lang> |
|||
The List class is the generic equivalent of the ArrayList class. |
|||
A List is a strongly typed list of objects that can be accessed by index ( zero-based again). |
|||
<lang csharp> |
|||
//Create and initialize List |
|||
List<string> myList = new List<string> { "Hello", "World", "!" }; |
|||
//Create List and add some values |
|||
List<string> myList2 = new List<string>(); |
|||
myList2.Add("Hello"); |
|||
myList2.Add("World"); |
|||
myList2.Add("!"); |
|||
</lang> |
|||
===Hashtable and Dictionary=== |
|||
Hashtables represent a collection of key/value pairs that are organized based on the hash code of the key. |
|||
Keys must be unique. |
|||
<lang csharp> |
|||
//Create an initialize Hashtable |
|||
Hashtable myHt = new Hashtable() { { "Hello", "World" }, { "Key", "Value" } }; |
|||
//Create Hashtable and add some Key-Value pairs. |
|||
Hashtable myHt2 = new Hashtable(); |
|||
myHt2.Add("Hello", "World"); |
|||
myHt2.Add("Key", "Value"); |
|||
</lang> |
|||
Dictionary is a generic class.It represents a collection of key/value pairs. Keys must be unique. |
|||
<lang csharp> |
|||
//Create an initialize Dictionary |
|||
Dictionary<string, string> dict = new Dictionary<string, string>() { { "Hello", "World" }, { "Key", "Value" } }; |
|||
//Create Dictionary and add some Key-Value pairs. |
|||
Dictionary<string, string> dict2 = new Dictionary<string, string>(); |
|||
dict2.Add("Hello", "World"); |
|||
dict2.Add("Key", "Value"); |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 688: | Line 688: | ||
collections.cob: 33: libcob: Subscript of 'sample-number' out of bounds: 3 |
collections.cob: 33: libcob: Subscript of 'sample-number' out of bounds: 3 |
||
</pre> |
</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 887: | Line 887: | ||
→ my-collection |
→ my-collection |
||
</lang> |
</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 5.0: |
ELENA 5.0: |
||
Line 1,206: | Line 1,207: | ||
nine |
nine |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Built in, resizable=== |
===Built in, resizable=== |
||
Line 2,125: | Line 2,127: | ||
{{output}} |
{{output}} |
||
<pre>[0, "foo", 3.141593, 42, 1, 2, 3]</pre> |
<pre>[0, "foo", 3.141593, 42, 1, 2, 3]</pre> |
||
=={{header|MS SmallBasic}}== |
|||
Only with LD extension |
|||
<lang MSsmallbasic> |
|||
ll=LDList.CreateFromValues("") |
|||
LDList.Add(ll "Cars") |
|||
LDList.Add(ll "Toys") |
|||
LDList.Print(ll) |
|||
</lang> |
|||
result: |
|||
List1 |
|||
1 : Cars |
|||
2 : Toys |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
Line 2,658: | Line 2,676: | ||
print $i . " -> " . $h{$i} . "\n"; |
print $i . " -> " . $h{$i} . "\n"; |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
Perl 6 has both mutable and immutable containers of various sorts. Here are some of the most common ones: |
|||
===Mutable=== |
|||
<lang perl6># Array |
|||
my @array = 1,2,3; |
|||
@array.push: 4,5,6; |
|||
# Hash |
|||
my %hash = 'a' => 1, 'b' => 2; |
|||
%hash<c d> = 3,4; |
|||
%hash.push: 'e' => 5, 'f' => 6; |
|||
# SetHash |
|||
my $s = SetHash.new: <a b c>; |
|||
$s ∪= <d e f>; |
|||
# BagHash |
|||
my $b = BagHash.new: <b a k l a v a>; |
|||
$b ⊎= <a b c>;</lang> |
|||
===Immutable=== |
|||
<lang perl6># List |
|||
my @list := 1,2,3; |
|||
my @newlist := |@list, 4,5,6; # |@list will slip @list into the surrounding list instead of creating a list of lists |
|||
# Set |
|||
my $set = set <a b c>; |
|||
my $newset = $set ∪ <d e f>; |
|||
# Bag |
|||
my $bag = bag <b a k l a v a>; |
|||
my $newbag = $bag ⊎ <b e e f>;</lang> |
|||
===Pair list (cons list)=== |
|||
<lang perl6>my $tail = d => e => f => Nil; |
|||
my $new = a => b => c => $tail;</lang> |
|||
===P6opaque object (immutable in structure)=== |
|||
<lang perl6>class Something { has $.foo; has $.bar }; |
|||
my $obj = Something.new: foo => 1, bar => 2; |
|||
my $newobj = $obj but role { has $.baz = 3 } # anonymous mixin</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,889: | Line 2,865: | ||
$list.RemoveAt(2) |
$list.RemoveAt(2) |
||
</lang> |
</lang> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 3,057: | Line 3,032: | ||
</lang> |
</lang> |
||
Racket comes with about 7000 additional types that can be considered as a collection of values, but it's not clear whether this entry is supposed to be a laundry list... |
Racket comes with about 7000 additional types that can be considered as a collection of values, but it's not clear whether this entry is supposed to be a laundry list... |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Perl 6 has both mutable and immutable containers of various sorts. Here are some of the most common ones: |
|||
===Mutable=== |
|||
<lang perl6># Array |
|||
my @array = 1,2,3; |
|||
@array.push: 4,5,6; |
|||
# Hash |
|||
my %hash = 'a' => 1, 'b' => 2; |
|||
%hash<c d> = 3,4; |
|||
%hash.push: 'e' => 5, 'f' => 6; |
|||
# SetHash |
|||
my $s = SetHash.new: <a b c>; |
|||
$s ∪= <d e f>; |
|||
# BagHash |
|||
my $b = BagHash.new: <b a k l a v a>; |
|||
$b ⊎= <a b c>;</lang> |
|||
===Immutable=== |
|||
<lang perl6># List |
|||
my @list := 1,2,3; |
|||
my @newlist := |@list, 4,5,6; # |@list will slip @list into the surrounding list instead of creating a list of lists |
|||
# Set |
|||
my $set = set <a b c>; |
|||
my $newset = $set ∪ <d e f>; |
|||
# Bag |
|||
my $bag = bag <b a k l a v a>; |
|||
my $newbag = $bag ⊎ <b e e f>;</lang> |
|||
===Pair list (cons list)=== |
|||
<lang perl6>my $tail = d => e => f => Nil; |
|||
my $new = a => b => c => $tail;</lang> |
|||
===P6opaque object (immutable in structure)=== |
|||
<lang perl6>class Something { has $.foo; has $.bar }; |
|||
my $obj = Something.new: foo => 1, bar => 2; |
|||
my $newobj = $obj but role { has $.baz = 3 } # anonymous mixin</lang> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
Line 4,018: | Line 4,036: | ||
<lang v>[4 3 2 1] 5 swap cons |
<lang v>[4 3 2 1] 5 swap cons |
||
=[5 4 3 2 1]</lang> |
=[5 4 3 2 1]</lang> |
||
=={{header|Vim Script}}== |
|||
Vim Script has two collection types: <code>List</code> and <code>Dictionary</code>. |
|||
See [[Arrays#Vim Script|Arrays]] for basic operations on a <code>List</code> and [[Associative_array/Creation#Vim Script|Associative_array/Creation]] for basic operations on a <code>Dictionary</code>. |
|||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Line 4,029: | Line 4,042: | ||
coll.Add "apple" |
coll.Add "apple" |
||
coll.Add "banana"</lang> |
coll.Add "banana"</lang> |
||
=={{header|Vim Script}}== |
|||
Vim Script has two collection types: <code>List</code> and <code>Dictionary</code>. |
|||
See [[Arrays#Vim Script|Arrays]] for basic operations on a <code>List</code> and [[Associative_array/Creation#Vim Script|Associative_array/Creation]] for basic operations on a <code>Dictionary</code>. |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Line 4,086: | Line 4,104: | ||
</lang> |
</lang> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
<lang zkl>Lists: L(1,2,3).append(4); //-->L(1,2,3,4), mutable list |
<lang zkl>Lists: L(1,2,3).append(4); //-->L(1,2,3,4), mutable list |
||
Line 4,097: | Line 4,116: | ||
Data(0,String,"foo ","bar") //-->9 bytes (2 \0s) |
Data(0,String,"foo ","bar") //-->9 bytes (2 \0s) |
||
Data(0,String,"foo ").append("bar").readln() //-->"foo "</lang> |
Data(0,String,"foo ").append("bar").readln() //-->"foo "</lang> |
||
=={{header|MS SmallBasic}}== |
|||
Only with LD extension |
|||
<lang MSsmallbasic> |
|||
ll=LDList.CreateFromValues("") |
|||
LDList.Add(ll "Cars") |
|||
LDList.Add(ll "Toys") |
|||
LDList.Print(ll) |
|||
</lang> |
|||
result: |
|||
List1 |
|||
1 : Cars |
|||
2 : Toys |