Jump to content

Collections: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
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.
 
=={{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++}}==
Line 536 ⟶ 601:
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>
 
=={{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}}==
Line 688:
collections.cob: 33: libcob: Subscript of 'sample-number' out of bounds: 3
</pre>
 
=={{header|Common Lisp}}==
 
Line 887:
→ my-collection
</lang>
 
=={{header|Elena}}==
ELENA 5.0:
Line 1,206 ⟶ 1,207:
nine
</pre>
 
=={{header|Go}}==
===Built in, resizable===
Line 2,125 ⟶ 2,127:
{{output}}
<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}}==
Line 2,658 ⟶ 2,676:
print $i . " -> " . $h{$i} . "\n";
}</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}}==
Line 2,889 ⟶ 2,865:
$list.RemoveAt(2)
</lang>
 
 
=={{header|Prolog}}==
Line 3,057 ⟶ 3,032:
</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...
 
=={{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}}==
Line 4,018 ⟶ 4,036:
<lang v>[4 3 2 1] 5 swap cons
=[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}}==
Line 4,029 ⟶ 4,042:
coll.Add "apple"
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}}==
Line 4,086 ⟶ 4,104:
 
</lang>
 
=={{header|zkl}}==
<lang zkl>Lists: L(1,2,3).append(4); //-->L(1,2,3,4), mutable list
Line 4,097 ⟶ 4,116:
Data(0,String,"foo ","bar") //-->9 bytes (2 \0s)
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
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.