Anonymous user
Array Initialization: Difference between revisions
→Haskell
m (remove Ruby) |
|||
(19 intermediate revisions by 5 users not shown) | |||
Line 1:
{{DeprecatedTask}}
'''Examples here should be migrated to [[Arrays]] or [[Creating an Associative Array]] and removed from here. If similar code already exists there, simply remove it from here.'''
Line 6 ⟶ 7:
See [[Creating_an_Array]] for this topic.
==
The array value obtained directly from data is called array aggregate. Considering these array declarations:
<lang ada>
Line 25 ⟶ 26:
</lang>
Note that the array bounds, when unconstrained as in these examples can be either determined by the aggregate, like the initialization of X shows. Or else they can be specified as a constraint, like for example in the initialization of Y. In this case '''others''' choice can be used to specify all unmentioned elements. But in any case, the compiler verifies that all array elements are initialized by the aggregate. Single dimensional arrays of characters can be initialized by character strings, as the variable S shows. Of course, array aggregates can participate in array expressions and these expressions can be used to initialize arrays. The variable B is initialized by an aggregate inversed by the operation '''not'''.
==[[C++]]==
===STL===
{{libheader|STL}}STL provides '''std::vector''', which behaves as a dynamically-resizable array. When an element is added, its value must be set immediately.
Line 128 ⟶ 69:
</lang>
==[[F_Sharp|F#]]==
let a = [| 1; 3; 5; 7; 9 |] // array of integers
let b = [| 1 .. 10 |] // initialize with range of integers
Line 277 ⟶ 75:
let d = [| "hello"; "world" |] // array of strings
==[[Haskell]]==
To create any new Array of the various array types, you can use this to initialise it with all elements filled with x, and indexes ranging from n to m
<lang haskell>
Line 363 ⟶ 89:
array ((0,0),(5,5)) [((0,0),0),((0,1),0),((0,2),0),((0,3),0),((0,4),0),((0,5),0),((1,0),0),((1,1),0),((1,2),0),((1,3),0),((1,4),0),((1,5),0),((2,0),0),((2,1),0),((2,2),0),((2,3),0),((2,4),0),((2,5),0),((3,0),0),((3,1),0),((3,2),0),((3,3),0),((3,4),0),((3,5),0),((4,0),0),((4,1),0),((4,2),0),((4,3),0),((4,4),0),((4,5),0),((5,0),0),((5,1),0),((5,2),0),((5,3),0),((5,4),0),((5,5),0)]
</lang>
=={{header|Scala}}==
<lang Scala>// immutable maps
var map = Map(1 -> 2, 3 -> 4, 5 -> 6)
map(3) // 4
map = map + (44 -> 99) // maps are immutable, so we have to assign the result of adding elements
map.isDefinedAt(33) // false
map.isDefinedAt(44) // true</lang>
<lang scala>// mutable maps (HashSets)
import scala.collection.mutable.HashMap
val hash = new HashMap[Int, Int]
hash(1) = 2
hash += (1 -> 2) // same as hash(1) = 2
hash += (3 -> 4, 5 -> 6, 44 -> 99)
hash(44) // 99
hash.contains(33) // false
hash.isDefinedAt(33) // same as contains
hash.contains(44) // true</lang>
<lang scala>// iterate over key/value
hash.foreach {e => println("key "+e._1+" value "+e._2)} // e is a 2 element Tuple
// same with for syntax
for((k,v) <- hash) println("key " + k + " value " + v)</lang>
<lang scala>// items in map where the key is greater than 3
map.filter {k => k._1 > 3} // Map(5 -> 6, 44 -> 99)
// same with for syntax
for((k, v) <- map; if k > 3) yield (k,v)</lang>
|