Basically, I have an array like this:
val base_length = Array(
0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
64, 80, 96, 112, 128, 160, 192, 224, 0
);
And when scala sees it, it wants to do this:
base_length: Array[Int] = Array(...)
But I would prefer for it to do this:
base_length: Array[Byte] = Array(...)
I tried:
val base_length = Array[Byte](...)
But scala says:
<console>:4: error: type arguments [Byte] do not conform to method apply's type
parameter bounds [A <: AnyRef]
val base_length = Array[Byte](1,2,3,4,5)
This seems to me to basically be telling me that the Array constructor wants to figure out what the type of the array is from the arguments. Normally that's awesome, but in this instance I have good reasons for wanting the array elements to be Byte
s.
I have looked around for guidance on this, but I don't seem to be able to find anything. Any help would be great!
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
Initializing ArraysArrays may be initialized when they are declared, just as any other variables. Place the initialization data in curly {} braces following the equals sign. Note the use of commas in the examples below. An array may be partially initialized, by providing fewer data items than the size of the array.
The Scala List class holds a sequenced, linear list of items. Following are the point of difference between lists and array in Scala: Lists are immutable whereas arrays are mutable in Scala.
It should be:
C:\prog\>scala
Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val gu: Array[Byte] = Array(18, 19, 20)
gu: Array[Byte] = Array(18, 19, 20)
This is not immutable. A Seq would be a step in that direction even if it is only a trait (as Christopher mentions in the comments) adding finite sequences of elements. A Scala List would be immutable.
Works in Scala 2.8.0:
Welcome to Scala version 2.8.0.r18502-b20090818020152 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.
scala> Array[Byte](0, 1, 2)
res0: Array[Byte] = Array(0, 1, 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With